sax.DocumentTracer and sax.Writer

This section provides tutorial examples on how to use sample programs sax.DocumentTracer and sax.Writer to validate XML documents with XSD schema. Their error reporting format is better than dom.GetElementsByTagName and dom.Writer.

If you want a better validation error report that shows the XML structure with errors located inside the structure, you can also try the 2 SAX sample programs. They also support XSD schema validation:

Here is my test result:

>jdk8x2r sax.DocumentTracer -v -s hello_xsd_invalid.xml

setDocumentLocator(locator=org.apache.xerces.parsers
^^^.AbstractSAXParser$LocatorProxy@1a52fbd)
startDocument()
 startPrefixMapping(prefix="xsi",uri="http://www.w3.org/2001
^^^/XMLSchema-instance")
 startElement(uri="",localName="p",qname="p",attributes={{uri=
^^^"http://www.w3.org/2001/XMLSchema-instance",localName=
^^^"noNamespaceSchemaLocation",qname="xsi:noNamespaceSchemaLocation",
^^^type="CDATA",value="hello.xsd"}})
  characters(text="\nHello ")
  startElement(uri="",localName="i",qname="i",attributes={})
   characters(text="world")
  endElement(uri="",localName="i",qname="i")
  characters(text="!")
[Error] hello_xsd_invalid.xml:4:24: cvc-type.3.1.2: Element 'p' is a 
^^^ simple type, so it must have no element information item 
^^^ [children].
 endElement(uri="",localName="p",qname="p")
 endPrefixMapping(prefix="xsi")
endDocument()


>jdk8x2r sax.Writer -v -s hello_xsd_invalid.xml

<?xml version="1.0" encoding="UTF-8"?>
<p xsi:noNamespaceSchemaLocation="hello.xsd">
Hello <i>world</i>![Error] hello_xsd_invalid.xml:4:24: cvc-type.3.1.2:
^^^ Element 'p' is a simple type, so it must have no element 
^^^ information item [children].
</p>

Cool. Both sax.DocumentTracer and sax.Writer can be used as validation tools on XML documents associated with XSD schema. Their error reporting format is better than dom.GetElementsByTagName and dom.Writer, since error messages are embedded inside the XML structure printout next to the place where errors occurred.

Between sax.Writer and sax.DocumentTracer, sax.Writer's output format is better, since it prints out the XML document in the original format with error messages embedded.

Last update: 2014.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

Xerces2 Java Parser - Java API of XML Parsers

 Installing Xerces2 Java Parser for XSD 1.1

 Verifying Installation of Xerces2

 Xerces2 Sample Program List

 Xerces2 Sample Program dom.Counter

 dom.Counter Validating XML with Associated XSD

 dom.GetElementsByTagName and dom.Writer

sax.DocumentTracer and sax.Writer

 Examples of XSD and XML Files with Errors

 sax.Writer Reporting Errors Embedded in XML Structure

 XSD 1.1 not Supported by sax.Writer

 XSD 1.1 Supported by jaxp.SourceValidator

 Examples of XSD 1.1 and XML Files with Errors

 jaxp.TypeInfoWriter as an XSD 1.1 Validation Tool

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version