[Soapui] How to Use schema to verify the XML file corresponding to response through the *. XSD File

Source: Internet
Author: User
Tags soapui groovy script

Add a groovy script to verify the test step.

The script is as follows (it has been run successfully ):

import javax.xml.XMLConstantsimport javax.xml.transform.stream.StreamSourceimport javax.xml.validation.SchemaFactory//Load the XSD from a filedef xsd = new File(‘D:\\DOAutomationTest\\Automation_Test_DO_IpadForAdvisor_SoapUI\\Schemas\\schema_Clients.xsd‘).textlog.info "xsd = "+xsd//Get the XML from the responsedef response = testRunner.testCase.testSteps["testStepName"].testRequest.response.contentAsStringlog.info "response = "+responsedef factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))def validator = schema.newValidator()try {validator.validate(new StreamSource(new StringReader(response)))} catch( Exception ) {assert false}

A sample that inspires me (or it can run successfully ):

class XmlExamples {  static def CAR_RECORDS = ‘‘‘    <records>      <car name=‘HSV Maloo‘ make=‘Holden‘ year=‘2006‘>        <country>Australia</country>        <record type=‘speed‘>Production Pickup Truck with speed of 271kph</record>      </car>      <car name=‘P50‘ make=‘Peel‘ year=‘1962‘>        <country>Isle of Man</country>        <record type=‘size‘>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>      </car>      <car name=‘Royale‘ make=‘Bugatti‘ year=‘1931‘>        <country>France</country>        <record type=‘price‘>Most Valuable Car at $15 million</record>      </car>    </records>  ‘‘‘}def xsd = ‘‘‘<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  <xs:element name="records">    <xs:complexType>      <xs:sequence>        <xs:element maxOccurs="unbounded" ref="car"/>      </xs:sequence>    </xs:complexType>  </xs:element>  <xs:element name="car">    <xs:complexType>      <xs:sequence>        <xs:element ref="country"/>        <xs:element ref="record"/>      </xs:sequence>      <xs:attribute name="make" use="required" type="xs:NCName"/>      <xs:attribute name="name" use="required"/>      <xs:attribute name="year" use="required" type="xs:integer"/>    </xs:complexType>  </xs:element>  <xs:element name="country" type="xs:string"/>  <xs:element name="record">    <xs:complexType mixed="true">      <xs:attribute name="type" use="required" type="xs:NCName"/>    </xs:complexType>  </xs:element></xs:schema>‘‘‘.trim()import javax.xml.XMLConstantsimport javax.xml.transform.stream.StreamSourceimport javax.xml.validation.SchemaFactorydef factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))def validator = schema.newValidator()validator.validate(new StreamSource(new StringReader(XmlExamples.CAR_RECORDS)))

  

[Soapui] How to Use schema to verify the XML file corresponding to response through the *. XSD File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.