In this blog post, we'll cover how to create an XML Validator to validate XML documents against an XSD (XML Schema Definition) schema file. I recently created a similar utility to assist a client with integration that required XML files sent over the government authority's SOAP API. We're using Java because, that's the preferred tech stack that was used by my client and well also, Java has a very good built-in libraries to work with XML files and to validate them. We'll use Java's built-in javax.xml.validation package to accomplish this task.
Step 1: Set up the XML and XSD Files
Before diving into the Java code, let's prepare a sample XML file and an XSD schema file to work with. Save these files to your project directory:
sample.xml:
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="1"> <name>John Doe</name> <age>30</age> <city>New York</city> </person> <person id="2"> <name>Jane Doe</name> <age>28</age> <city>Los Angeles</city> </person> </persons>
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="city" type="xs:string"/> </xs:sequence> <xs:attribute name="id" type="xs:integer" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Step 2: Create the Java XML Validator
Now that we have our XML and XSD files, let's create a Java XML Validator using the javax.xml.validation package.
import java.io.File; import java.io.IOException; import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; public class XMLValidator { public static void main(String[] args) { if (args.length != 2) { System.err.println("Usage: java XMLValidator <xml-file> <xsd-file>"); System.exit(1); } String xmlFilePath = args[0]; String xsdFilePath = args[1]; try { validateXML(xmlFilePath, xsdFilePath); System.out.println("The XML file is valid."); } catch (SAXException | IOException e) { System.err.println("Error: " + e.getMessage()); } } public static void validateXML(String xmlFilePath, String xsdFilePath) throws SAXException, IOException { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(new File(xsdFilePath)); Validator validator = schema.newValidator(); Source source = new StreamSource(new File(xmlFilePath)); validator.validate(source); } }
Step 3: Compile and Run the Java XML Validator
Compile the XMLValidator.java file:
javac XMLValidator.java
Run the compiled Java XML Validator with the sample XML and XSD files as arguments:
java XMLValidator sample.xml schema.xsd
If the XML file is valid against the XSD schema, you'll see the following output:
The XML file is valid.
If the XML file is not valid, the validator will throw an exception with a relevant error message. For example, if you modify the sample.xml file to include an invalid element, you might see an error message like this:
Error: cvc-complex-type.2.4.a: Invalid content was found starting with element 'invalidElement'. One of '{"":name, "":age, "":city}' is expected.
That's it! You've created a Java XML Validator that can validate XML files against an XSD schema. This simple utility can be easily integrated into your projects, enabling you to ensure your XML files adhere to a specific structure and data format.