Main Page

Previous Section Next Section

JAXP and XML Schemas

An XML document can optionally be associated with the XML schema or a DTD defining its structure. While parsing the document with SAX, a DTD, as mentioned earlier, can be parsed and then validated using the DTDHandler. A special handler like a DTDHandler is needed is because DTDs are not XML documents.

With Web services, XML schemas are a preferred mechanism for defining the data. Details about DTDs, schemas, and namespaces can be found in Appendix A.

Let us now look at how JAXP works for validating XML against the schema. JAXP 1.1 did not expose any schema-related functionality at the API level. It was up to the implementation to support schema validation (the Xerces-j parsers or the reference implementation did validate schemas when validation and namespace processing were enabled).

Even though a schema may be associated with an XML document, the W3C recommendations on validating the document against schemas do not mandate a particular mechanism for actually locating the schemas. This makes sense, because there might be cases where the schemas are already available and known (e.g., under business agreements between business partners), or such validation may never be feasable (e.g., XML parsing on J2ME devices).

To give applications flexibility and to follow the suggested W3C recommendations, JAXP 1.2 introduces two additional properties that can be passed to the SAXParser using the setProperty() method and to DocumentBuilderFactory using the setAttribute() method:

JAXP behavior for parsing and validating schemas with these properties is in line with the W3C recommendation. Figure 9.4 summarizes the flow.

Click To expand
Figure 9.4: XML validation with JAXP 1.2

Let us look at an example of validating XML based on its schema. Listing 9.4a shows the XML from Listing 9.1 with a schema associated with it. Listing 9.4b shows the schema itself. Listing 9.4c shows the code required to validate the schema against the XML document. It differs from the previous code only in that the properties have been set and the factory told to return a namespace-aware, validating parser.

Listing 9.4a: XML document with schema and namespaces
Start example
<?xml version="1.0"?>
<contacts:flutebank xmlns="" xmlns:xsi="http://" xmlns:contacts=""
xsi:schemaLocation=" saxexample2.xsd">
   <contacts:administrator type="maintenance" level="support-1">
      <contacts:lastname> Malkovich</contacts:lastname>
End example
Listing 9.4b: The schema for the XML document
Start example
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="" xmlns="http://" xmlns:xs=""
   <xs:element name="flutebank">
            <xs:element ref="administrator"/>
   <xs:element name="administrator">
            <xs:element ref="firstname"/>
            <xs:element ref="lastname"/>
            <xs:element ref="telephone"/>
            <xs:element ref="email"/>
         <xs:attribute name="type" type="xs:string" use="required"/>
         <xs:attribute name="level" type="xs:string" use="required"/>
   <xs:element name="firstname" type="xs:string"/>
   <xs:element name="lastname" type="xs:string"/>
   <xs:element name="telephone">
            <xs:element ref="pager"/>
            <xs:element ref="cellular"/>>
            <xs:element ref="desk"/>
   <xs:element name="pager" type="xs:string"/>
   <xs:element name="cellular" type="xs:string"/>
   <xs:element name="desk" type="xs:string"/>
   <xs:element name="email">
            <xs:element ref="work"/>
            <xs:element ref="personal"/>
   <xs:element name="work" type="xs:string"/>
   <xs:element name="personal" type="xs:string"/>
End example
Listing 9.4c: The example from Listing 9.2a modified for schema validation
Start example
public class SAXParsingWithSchemas {
    public static void main(String[] arg) {
        try {
        // Create a new factory that will create the parser.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
// If needed the schema source can be set explicitly as shown below. This example uses
// the schema associated with the XML document
//       parser.setProperty("",
//                           , new InputSource(someuri));
         DefaultHandler handler = new MySAXHandler();
         parser.parse( new File(filename), handler);
         } catch (Exception e) {

End example

Previous Section Next Section

JavaScript Editor Java Tutorials Free JavaScript Editor