Forum Discussion

ATokeley's avatar
ATokeley
Contributor
14 years ago

RE: Groovy XML Validation with multiple imported schemas

Hi guys,
could you help me figure out how to setup and use the xerces lib when validating against multiple XSDs. Here is an existing thread on the subject, although it is missing details on how to implement the xerces lib. The javax one doesn't play nice when trying to validate against an array of XSDs..

http://www.eviware.com/forum/viewtopic.php?f=5&t=5217&p=16596&hilit=StreamSource#p16596

Many thanks,
Richard

5 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Richard,

    soapUI itself uses XMLBeans for all its xsd validations, could you use its APIs instead?

    regards,

    /Ole
    eviware.com
  • Does this code make sense to you guys? It runs without error, but doesn't seem to be validating my response xml correctly. All feedback welcome!


    import java.util.ArrayList
    import javax.xml.XMLConstants
    import javax.xml.transform.Source
    import javax.xml.transform.stream.StreamSource
    import org.xml.sax.InputSource;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    def resultXML = context.expand('${#resultsXML}' )
    def schemaPath = context.expand ( '${#schemaPath}' )
    def schemaFiles = context.expand ( '${#schemaFiles}' )

    def schemaSources = new ArrayList()
    def st = new StringTokenizer(schemaFiles,';')
    while (st.hasMoreTokens()) { schemaSources.add( new StreamSource(new FileInputStream(schemaPath + st.nextToken() ) ) ) }

    def schemaFactory = javax.xml.validation.SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    def schema = schemaFactory.newSchema((Source[])schemaSources.toArray());
    def validator = schema.newValidator();

    def stringReader = new StringReader(resultXML);
    def inputSource = new InputSource(stringReader);


    // set it so that it is validating and namespace aware
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating( true );
    parserFactory.setNamespaceAware( true );
    parserFactory.setSchema( schema );

    def parseHandler = new org.xml.sax.helpers.DefaultHandler();

    try {
    log.info "Starting to parse file...";
    def saxReader = parserFactory.newSAXParser();
    saxReader.parse(inputSource, parseHandler);
    log.info "Done parsing file...";
    }
    catch (Exception e) {
    log.info(e);
    }
  • The issue I'm facing is that our response relies on multiple XSDs for its def. Therefore I need to pull them into some sort of reference array. I've tried several options I've found online but am now blocked. Do you guys have the bandwidth to help out with a sample project using validation with multiple xsds?

    Cheers,
    - Richard