Reputation: 707
I want to validate an XML file while it's being parsed. Stand-alone validation with Validator.validate()
says it's OK, no exception is thrown while parsing, but the [overridden] error()
method from the parsing handler gets called. Why? Is there some state I need to initialize? If I deliberately make the XML file incorrect, stand-alone validation will fail and if I comment out validation, parsing also fails -- both with SAXParseException, and with the error I expected.
[code below edited down from actual stuff with lots of println's to illustrate the issue]
short.xsd:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myapp.com/sample"
xmlns:smp="http://www.myapp.com/sample"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Sample" type="xs:string"/>
</xs:schema>
short.xml:
<?xml version="1.0"?>
<Sample
xmlns="http://www.myapp.com/sample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.myapp.com/sample shortxsd.xsd"
>
hello
</Sample>
Source:
import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public static void main(String... args)
throws IOException
{
String xsdFileName = "shortxsd.xsd";
URL xsdURL = Thread.currentThread().getContextClassLoader().getResource(xsdFileName);
String xsdPath = xsdURL.getPath();
System.out.println("xsdFileName: " + xsdFileName);
System.out.println("xsdURL: " + xsdURL);
System.out.println("xsdPath: " + xsdPath);
String xmlFileName = "shortxml.xml";
URL xmlURL = Thread.currentThread().getContextClassLoader().getResource(xmlFileName);
String xmlPath = xmlURL.getPath();
System.out.println("xmlFileName: " + xmlFileName);
System.out.println("xmlURL: " + xmlURL);
System.out.println("xmlPath: " + xmlPath);
/* Schema creation: */
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
schema = schemaFactory.newSchema(new File(xsdURL.getFile()));
}
catch (SAXException ex) {
System.out.println("Schema creation exception: " + ex);
return;
}
System.out.println("+ Schema creation OK");
/* Stand-alone Validation: */
Validator validator = schema.newValidator();
Source xmlFile = new StreamSource(xmlURL.openStream());
try {
validator.validate(xmlFile);
}
catch (SAXException ex) {
System.out.println("Validation exception: " + ex);
return;
}
System.out.println("+ Stand-alone Validation OK");
/* Parsing with validation: */
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setSchema(schema);
SAXParser saxParser = null;
try {
saxParser = parserFactory.newSAXParser();
}
catch (ParserConfigurationException |
SAXException ex) {
System.out.println("Parser creation exception: " + ex);
return;
}
System.out.println("+ Parser creation OK");
try (InputStream xmlInput = xmlURL.openStream()) {
saxParser.parse(xmlInput, new DefaultHandler()
{
@Override
public void error(SAXParseException e)
{
System.out.println("# Error: " + e);
}
});
}
catch (SAXException ex) {
System.out.println("Parsing exception: " + ex);
return;
}
System.out.println("+ Parsing OK");
}
Output:
xsdFileName: shortxsd.xsd
xsdURL: file:/.../target/classes/shortxsd.xsd
xsdPath: /.../target/classes/shortxsd.xsd
xmlFileName: shortxml.xml
xmlURL: file:/.../target/classes/shortxml.xml
xmlPath: /.../target/classes/shortxml.xml
+ Schema creation OK
+ Stand-alone Validation OK
+ Parser creation OK
# Error: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 2; cvc-elt.1: Cannot find the declaration of element 'Sample'.
+ Parsing OK
Upvotes: 0
Views: 1870
Reputation: 64959
You need to configure your SAX parser to be namespace-aware.
If you remove the xmlns="..."
attribute from your XML document, and the xmlns:smp="..."
and targetNamespace="..."
attributes from your XML schema, your code does not produce any error messages. So the problem has to be something to do with namespaces.
To make the SAX parser namespace-aware, set an option in the factory:
/* Parsing with validation: */
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true); // add this line
parserFactory.setSchema(schema);
I made this change to your code and it printed out four OK messages at the end.
Upvotes: 2