How do I validate XML against XSD in Java?

The javax.xml.validation package provides an API for XML documents validation. The validation process verify that an XML document is an instance of a specified XML schema file or XSD file. In this example we are going to validate if the records.xml file below ins an instance of the records.xsd schema. First we will create the following XML file and an XSD file it should follow.

The XML file:

<?xml version="1.0" encoding="UTF-8"?>
            <title>Brand New Eyes</title>
            <genre>Punk Rock</genre>
            <artist>Various Artist</artist>

The XSD file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="" attributeFormDefault="unqualified"
    <xs:element name="root" type="rootType">

    <xs:complexType name="rootType">
            <xs:element name="records" type="recordsType"/>

    <xs:complexType name="recordsType">
            <xs:element name="record" type="recordType" maxOccurs="unbounded" minOccurs="0"/>

    <xs:complexType name="recordType">
            <xs:element type="xs:string" name="title"/>
            <xs:element type="xs:string" name="artist"/>
            <xs:element type="xs:string" name="genre"/>
            <xs:element type="xs:short" name="year"/>

The code snippet below will handle the validation process in the following steps. In the main() method we create the XMLValidator instance and call the validate() method and pass the XML file and the XSD file. Our validate() method start by creating an instance of SchemaFactory. The SchemaFactory.newInstance() method return an instance of SchemaFactory. In this example we are create a W3C XML Schema.

The next step is to create a Schema object by calling the schemaFactory.newSchema() and pass the schema / XSD file. The Schema object will allow us the create an instance of javax.xml.validation.Validator by calling the schema.newValidator() method. And finally to validate if the XML is valid we call validator.validate() method and pass the XML file to be validated. If the XML is not valid this validate method will throw exceptions.

The Java code:

package org.kodejava.example.xml;

import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.util.Objects;

public class XMLValidator {
    public static final String XML_FILE = "records.xml";
    public static final String SCHEMA_FILE = "records.xsd";

    public static void main(String[] args) {
        XMLValidator XMLValidator = new XMLValidator();
        boolean valid = XMLValidator.validate(XML_FILE, SCHEMA_FILE);

        System.out.printf("%s validation = %b.", XML_FILE, valid);

    private boolean validate(String xmlFile, String schemaFile) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            Schema schema = schemaFactory.newSchema(new File(getResource(schemaFile)));

            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(getResource(xmlFile))));
            return true;
        } catch (SAXException | IOException e) {
            return false;

    private String getResource(String filename) throws FileNotFoundException {
        URL resource = getClass().getClassLoader().getResource(filename);

        return resource.getFile();

How do I convert JDOM Document to String?

This example demonstrate how to convert JDOM Document object to a String using the XMLOutputter.outputString(Document doc) method.

package org.kodejava.example.jdom;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class JDOMDocumentToString {
    public static void main(String[] args) {
        Document document = new Document();
        Element root = new Element("rows");

        // Creating a child for the root element. Here we can see how to
        // set the text of an xml element.
        Element child = new Element("row");
        child.addContent(new Element("firstname").setText("Alice"));
        child.addContent(new Element("lastname").setText("Mallory"));
        child.addContent(new Element("address").setText("Sunset Road"));

        // Add the child to the root element and add the root element as
        // the document content.

        // Create an XMLOutputter object with pretty formatter. Calling
        // the outputString(Document doc) method convert the document
        // into string data.
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        String xmlString = outputter.outputString(document);

The result of our code snippet:

<?xml version="1.0" encoding="UTF-8"?>
    <address>Sunset Road</address>

Maven Dependencies

<!-- -->

How do I get mixed content of an XML element in JDOM?

The following examples demonstrate how to read mixed content of an xml element. A mixed content can have more than one type of content such as text (Text), comment (Comment), CDATA (CDATA) or some child elements (Element).

You also see that we can remove the mixed content from the element by calling the remove method of the List just like what we do with a collection of data.

package org.kodejava.example.jdom;

import org.jdom2.*;
import org.jdom2.input.SAXBuilder;

import java.util.List;

public class JDOMMixedContent {
    public static void main(String[] args) {
        String xml = "<root>" +
                "    <data>" +
                "        <!-- This element contains application data -->" +
                "        User Information" +
                "        <![CDATA[<table><tr><td>-data-</td></tr></table>]]>" +
                "        <field name=\"username\">alice</field>" +
                "    </data>" +

        SAXBuilder builder = new SAXBuilder();
        try {
            Document document = StringReader(xml));
            Element root = document.getRootElement();
            Element data = root.getChild("data");

            // Reading the mixed content of an xml element and iterate
            // the result list. This list object can contains any of the
            // following objects: Comment, Element, CDATA, DocType,
            // ProcessingInstruction, EntityRef and Text.
            List content = data.getContent();

            for (Object o : content) {
                if (o instanceof Comment) {
                    Comment comment = (Comment) o;
                    System.out.println("Comment   = " + comment);
                } else if (o instanceof Element) {
                    Element element = (Element) o;
                    System.out.println("Element   = " + element);
                } else if (o instanceof CDATA) {
                    CDATA cdata = (CDATA) o;
                    System.out.println("CDATA     = " + cdata);
                } else if (o instanceof DocType) {
                    DocType docType = (DocType) o;
                    System.out.println("DocType   = " + docType);
                } else if (o instanceof ProcessingInstruction) {
                    ProcessingInstruction pi = (ProcessingInstruction) o;
                    System.out.println("PI        = " + pi);
                } else if (o instanceof EntityRef) {
                    EntityRef entityRef = (EntityRef) o;
                    System.out.println("EntityRef = " + entityRef);
                } else if (o instanceof Text) {
                    Text text = (Text) o;
                    System.out.println("Text      = " + text);

            // Remove the second mixed content which is the CDATA content.
        } catch (JDOMException | IOException e) {

Here are the result of our program:

Text      = [Text:         ]
Comment   = [Comment: <!-- This element contains application data -->]
Text      = [Text:         User Information        ]
CDATA     = [CDATA: <table><tr><td>-data-</td></tr></table>]
Text      = [Text:         ]
Element   = [Element: <field/>]
Text      = [Text:     ]

Maven Dependencies

<!-- -->