Validate XML against XSD Schema using JDOM
An XSD Schema is used to validate an XML document, it describes what elements, attributes, types are allowed and in which order. In the following tutorial we explain how you can validate an XML document against an XSD Schema using JDOM. We see two examples, first we see how you can validate against an XSD schema with a reference inside the XML document. The second example we show how to validate an XML document against an external XSD Schema.
XML Document with XSD Reference Inside
Suppose we have the following example.xml
file, which we want to validate. You can see that we include the http://www.w3.org/2001/XMLSchema-instance
namespace with prefix xsi
. Next we include a reference to the XSD Schema using the xsi:noNamespaceSchemaLocation
and provide the relative location of the XSD Schema. So both the XSD schema and the XML document are located in the same folder.
<?xml version="1.0" encoding="UTF-8"?>
<courses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="example.xsd">
<course id="1">
<name>JDOM XSD Validation</name>
<price>0.0</price>
</course>
<course id="2">
<name>Validate XML against XSD Schema</name>
<price>0.0</price>
</course>
<course id="3">
<name>
<![CDATA[
characters & ? $ ^ with markup
]]>
</name>
<price>12.0</price>
</course>
</courses>
XSD Schema
Next, we have the example.xsd
schema. This schema describes which elements, attributes and types are valid in the previous XML document. First, we create an element with the name courses
. This is the top level element of the XML file. In this element we could have multiple course
elements. This course
element can have a name
element of type string
and a price
element of type double
. Finally, the course
element must have an id
attribute of type string
.
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="courses">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="course" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:all>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="price" type="xsd:double"/>
</xsd:all>
<xsd:attribute name="id" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD Schema Validation Inside XML
If the XML document contains a reference to the XSD schema – either by noNamespaceSchemaLocation
or schemaLocation
– then you can validate the XML using the following code. First, we read the XML file. Next, we create a SAXBuilder
and pass the XMLReaders.XSDVALIDATING
argument in the constructor. Finally, when we execute the SAXBuilder#build()
method, the XML will be validated against the internal XSD schema reference. If the validation failed, a JDOMParseException
is thrown, containing the message of what has gone wrong.
package com.memorynotfound.xml.jdom;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import java.io.IOException;
import java.net.URL;
public class ValidateXmlAgainstXsd {
public static void main(String... args) throws JDOMException, IOException {
// read file from classpath
URL xml = ValidateXmlAgainstXsd.class.getResource("/example.xml");
SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
Document document = builder.build(xml);
System.out.println("Root: " + document.getRootElement().getName());
}
}
External XSD Schema Validation
When the XML document does not contain a XSD schema reference, we can also validate against an external XSD schema. First, we read both XML and XSD schema from the class path. Next, we create a XMLReaderJDOMFactory
and pass the XSD schema as an argument to the constructor. Next, we create the SAXBuilder
and pass the previously created factory in the constructor. When we execute the SAXBuilder#build(xml)
method, the XML document is validated against the external XSD schema. If the validation failes, a JDOMParseException
will be thrown.
package com.memorynotfound.xml.jdom;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderJDOMFactory;
import org.jdom2.input.sax.XMLReaderXSDFactory;
import java.io.IOException;
import java.net.URL;
public class ValidateXmlAgainstExternalXsd {
public static void main(String... args) throws JDOMException, IOException {
URL xml = ValidateXmlAgainstExternalXsd.class.getResource("/example.xml");
URL xsd = ValidateXmlAgainstExternalXsd.class.getResource("/example.xsd");
XMLReaderJDOMFactory factory = new XMLReaderXSDFactory(xsd);
SAXBuilder builder = new SAXBuilder(factory);
Document document = builder.build(xml);
System.out.println("root: " + document.getRootElement().getName());
}
}