DomXmlXsValidation.java - DOM Parser with XS Validation

Q

How to parse an XML file with XS (XML Schema) validation using the DOM API?

✍: FYIcenter

A

if you want to parse an XML file with XS (XML Schema) validation using the DOM API, you can follow these suggestions:

1. Set the validation flag to true on DocumentBuilderFactory object:

      DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
	  f.setValidating(Boolean.parseBoolean(args[1]));

2. Set XML schema language to be XML Schema and turn Namespace Awareness on:

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

3. Provide the XML file with XML Schema location (User.xsd) provided, UserXsdError.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<fyi:User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:fyi="http://fyicenter.com"
  xsi:schemaLocation="http://fyicenter.com User.xsd">
    <fyi:ID>ONE</fyi:ID>
    <fyi:Name>Frank Y. Ivy</fyi:Name>
    <fyi:BirthDate>1970-01-01+00:01</fyi:BirthDate>
    <fyi:Sex>  Male</fyi:Sex>
</fyi:User>

Here is a complete example of parsing an XML with DTD validation using SAX API, DomXmlXsValidation.java:

// Copyright (c) 2017 FYIcenter.com
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class DomXmlXsValidation {
   static String dot = "............................................................";
   public static void main(String[] args) throws Exception {
      DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
      f.setNamespaceAware(true);
	  f.setValidating(Boolean.parseBoolean(args[1]));
      f.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
	     "http://www.w3.org/2001/XMLSchema");
	  
      DocumentBuilder b = f.newDocumentBuilder();
      Document d = b.parse(new File(args[0]));
      System.out.println("Implementation class:\n   "+d.getClass().getName());

      System.out.println("DOM object elements and text contents:");
	  Node n = d.getDocumentElement();
	  printText(n, 1);
   }
   public static void printText(Node n, int l) {
      String v = "";
      if (n.getNodeType()==Node.TEXT_NODE) v = n.getTextContent();
      System.out.println(dot.substring(0,l)+n.getNodeName()+":"+v);
	  NodeList c = n.getChildNodes();
	  for (int i=0; i<c.getLength(); i++) {
	     printText(c.item(i),l+1);
	  }
   }
}

Compile and run the example program, DomXmlXsValidation.java:

>\fyicenter\jdk-1.8.0\bin\javac DomXmlXsValidation.java

>\fyicenter\jdk-1.8.0\bin\java DomXmlXsValidation UserXsdError.xml true

Warning: validation was turned on but an org.xml.sax.ErrorHandler was not set, 
which is probably not what is desired.  Parser will use a default ErrorHandler 
to print the first 0  errors.  Please call the setErrorHandler method to fix this.

Error: URI=file:UserXsdError.xml Line=6: cvc-datatype-valid.1.2.1: 'ONE' is not a valid value for 'integer'.
Error: URI=file:UserXsdError.xml Line=6: cvc-type.3.1.3: The value 'ONE' of element 'fyi:ID' is not valid.
Error: URI=file:UserXsdError.xml Line=8: cvc-datatype-valid.1.2.1: '1970-01-01+00:01' is not a valid value for 'dateTime'.
Error: URI=file:UserXsdError.xml Line=8: cvc-type.3.1.3: The value '1970-01-01+00:01' of element 'fyi:BirthDate' is not valid.

Implementation class:
   com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
DOM object elements and text contents:
.fyi:User:
..#text:

..fyi:ID:
...#text:ONE
..#text:

..fyi:Name:
...#text:Frank Y. Ivy
..#text:

..fyi:BirthDate:
...#text:1970-01-01+00:01
..#text:

..fyi:Sex:
...#text:Male
..#text:

If you run it again with "false", you will not see any validation errors:

>\fyicenter\jdk-1.8.0\bin\java DomXmlXsValidation UserXsdError.xml false

Implementation class:
   com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
DOM object elements and text contents:
.fyi:User:
..#text:

..fyi:ID:
...#text:ONE
..#text:

..fyi:Name:
...#text:Frank Y. Ivy
..#text:

..fyi:BirthDate:
...#text:1970-01-01+00:01
..#text:

..fyi:Sex:
...#text:  Male
..#text:

The XML Schema file used in the above test is, User.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:fyi="http://fyicenter.com"
   targetNamespace="http://fyicenter.com"
   elementFormDefault="qualified">

  <xsd:element name="User">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="ID" type="xsd:integer"/>
        <xsd:element name="Name" type="xsd:string"/>
        <xsd:element name="BirthDate" type="xsd:dateTime"/>
        <xsd:element name="Sex" type="fyi:sexType"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:simpleType name="sexType">
    <xsd:restriction base="xsd:NMTOKEN">
      <xsd:enumeration value="Male"/>
      <xsd:enumeration value="Femal"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

 

Using XML DOM API with Apache Xerces

⇒⇒FAQ for Apache Xerces XML Parser

2017-12-13, 509👍, 0💬