0

I am using XML schema validation to validate XML response against XSD schema. Problem is that XML validation is stopped after first error found.

For example, i have one error at line 105 and second at line 1200 It returns only error at line 105, then i have to fix this, or add value to XSD schema, only then i can see error at line 1200.

Is it possible to see all errors in one report?

1 Answer 1

1

XML Schema Assertion uses javax.xml.parsers.DocumentBuilder.parse() method which stops parsing XML file after the first error.

You can work it around by implementing your own ErrorHandler in i.e. Beanshell Assertion

  1. Add Beanshell Assertion instead of the XML Schema Assertion
  2. Put the following code into the Beanshell Assertion "Script" area:

    import org.xml.sax.ErrorHandler;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    
    import javax.xml.XMLConstants;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    import java.io.File;
    import java.util.LinkedList;
    import java.util.List;
    
    
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new StreamSource(new File("/path/to/your/schema.xsd")));
    Validator validator = schema.newValidator();
    final List exceptions = new LinkedList();
    validator.setErrorHandler(new ErrorHandler() {
    
        public void warning(SAXParseException exception) throws SAXException {
            exceptions.add(exception);
        }
    
        public void fatalError(SAXParseException exception) throws SAXException {
            exceptions.add(exception);
        }
    
        public void error(SAXParseException exception) throws SAXException {
            exceptions.add(exception);
        }
    });
    StreamSource xmlFile = new StreamSource(new StringReader(SampleResult.getResponseDataAsString()));
    validator.validate(xmlFile);
    
    if (exceptions.size() > 0) {
        Failure = true;
        for (int i = 0; i < exceptions.size(); i++) {
            FailureMessage += "Error " + (i + 1) + " : " + exceptions.get(i).getMessage();
            FailureMessage += System.getProperty("line.separator");
    
        }
    }
    
  3. Replace /path/to/your/schema.xsd with the actual path to XSD schema.

Remember that it may NOT return all the errors in case of fatal errors, according to ErrorHandler class JavaDoc:

Note, however, that there is no requirement that the parser continue to report additional errors after a call to fatalError. In other words, a SAX driver class may throw an exception after reporting any fatalError.

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java API in Beanshell Test Elements.

1
  • i am receiving following error: BeanShell Assertion : org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml . . . '' : Typed variable declaration : Final variable, can't re-assign.
    – ivanz
    Mar 31, 2016 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.