Forum Discussion

harsh1's avatar
harsh1
New Contributor
11 years ago

How to validate XML against the XSD by using groovy script in SOAP UI Pro?

1 Reply

  • Hi Harsh1,

     

    Please check if the following sample code posted to stackoverflow helps you:

     

    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory
    
    xsdUrl = 'http://abbot.sourceforge.net/doc/abbot.xsd'
    xmlUrl = 'http://abbot.sourceforge.net/src/example/SimpleApplet.xml'
    
    new URL( xsdUrl ).withInputStream { xsd ->
      new URL( xmlUrl ).withInputStream { xml ->
        SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI )
                     .newSchema( new StreamSource( xsd ) )
                     .newValidator()
                     .validate( new StreamSource( xml ) )
    
      }
    }