Show TOC Start of Content Area

Process documentation XML Schema Validation  Locate the document in its SAP Library structure

Purpose

The XML parser validates against the XML Schema using the http://java.sun.com/xml/jaxp/properties/schemaLanguage and http://java.sun.com/xml/jaxp/properties/schemaSource properties.

Process Flow

The http://java.sun.com/xml/jaxp/properties/schemaLanguage property defines the schema language to be used for validation. This property has to be mapped to a specific URI, associated with the XML schema specifications described at www.w3.org/2001/XMLSchema. When the validation option is set to “true” and the schema language is specified, the parser has to validate against the schema even if the doctype is not specified in the corresponding XML instance file. If schemaLocation or noNamespaceSchemaLocation attribute is in the XML file, then it can be used for specifying the location of the schema file.

The http://java.sun.com/xml/jaxp/properties/schemaSource property enables the user to specify the location(s) of the schema source(s) to validate against. This property has to be mapped to an object, associated with one of the following Java classes:

·        java.lang.String – schema URI

·        java.io.InputStream – schema input stream

·        org.xml.sax.InputSource – schema input source

·        java.io.File – schema file

·        an array of the described objects

It is not correct to set http://java.sun.com/xml/jaxp/properties/schemaSource property without setting the schema language for validation. If you do that, SAXNotSupportedException is thrown. If an entity resolver and the schema location are set using a string object, then this object has to be passed as a parameter to the resolve method associated with this entity resolver.

Note

Entity resolver is an object that implements the org.xml.sax.EntityResolver interface.

None of the properties take effect if the setValidating(true) method of SAXParserFactory or DocumentBuilderFactory is not invoked.

Example

·        SAX parsing:

Syntax

try {

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

saxParserFactory.setNamespaceAware(true);

saxParserFactory.setValidating(true);

SAXParser saxParser = saxParserFactory.newSAXParser();

saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "./xsd_example/xsd/example.xsd");

DefaultHandler handler = new DefaultHandler();

saxParser.parse("./xsd_example/xml/example.xml", handler);

} catch(SAXException exc) {

exc.printStackTrace();

}

 

·        DOM parsing:

Syntax

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

docBuilderFactory.setNamespaceAware(true);

docBuilderFactory.setValidating(true);

docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "./xsd_example/xsd/example.xsd");

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse("./xsd_example/xml/example.xml");

} catch(DOMException exc) {

exc.printStackTrace();

}

 

 

End of Content Area