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:
sax.DocumentTracer - Printing out parsing events in tree format using SAX technology.
sax.Writer - Writing back elements, attributes, and contents using SAX technology.
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.