This section provides tutorial examples on how to use sample programs dom.GetElementsByTagName and dom.Writer to validate XML documents with XSD schema.
You can also try the other 2 DOM sample programs. They all support XSD schema validation:
dom.GetElementsByTagName - Listing elements with their names and attributes using DOM technology.
dom.Writer - Writing back elements, attributes, and contents using DOM technology.
Here is my test result:
>jdk8x2r dom.GetElementsByTagName -v -s hello_xsd_invalid.xml
[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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
^^^ xsi:noNamespaceSchemaLocation="hello.xsd">
<i>
>jdk8x2r dom.Writer -v -s hello_xsd_invalid.xml
[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].
<?xml version="1.0" encoding="UTF-8"?>
<p xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
^^^ xsi:noNamespaceSchemaLocation="hello.xsd">
Hello <i>world</i>!</p>
Both dom.GetElementsByTagName and dom.Writer report
XSD schema validation errors correctly.
But errors are reported before the print out of the XML document information.
This is because they are using the DOM technology, which builds the entire XML document object
first before applying any validations.