Java XML validation with schema code examples
April 26, 2011 21:25:55 Last update: April 27, 2011 11:08:57
- The following code validates a
web.xmlfile against the web-app 2.5 schema:// Java XML validation with schema import java.io.*; import java.net.*; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.dom.DOMSource; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import javax.xml.validation.Validator; import javax.xml.parsers.*; import org.w3c.dom.Document; public class XMLValidation { public static void main(String[] args) throws Exception { String xmlFile = "web.xml"; URL schemaUrl = new URL("http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"); // Create schema SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = xsdFactory.newSchema(schemaUrl); // validate Validator validator = schema.newValidator(); validator.validate( new StreamSource(new File(xmlFile)) ); /* Alternative validation with DOMSource DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = parser.parse(new File(xmlFile)); validator.validate(new DOMSource(document)); */ } }
web.xmlused for testing:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- Appliocation name and description --> <display-name>Example App</display-name> <description>A web.xml example</description> <!-- Set session timeout to 30 minutes --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- Servlets --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
- According to Java API doc, validation can also be done while parsing by calling
setSchemaon the parsing factory. However, validation doesn't work for the SAXParserFactory!// Java XML validation with schema import java.io.*; import java.net.*; import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.dom.DOMSource; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import javax.xml.validation.Validator; import javax.xml.parsers.*; import org.w3c.dom.Document; public class XMLValidation2 { public static void main(String[] args) throws Exception { String xmlFile = "persistence.xml"; URL schemaUrl = new URL("http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"); // Create schema SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = xsdFactory.newSchema(schemaUrl); // parse and validate DocumentBuilderFactory df = DocumentBuilderFactory.newInstance(); df.setValidating(false); df.setNamespaceAware(true); df.setSchema(schema); DocumentBuilder builder = df.newDocumentBuilder(); Document doc = builder.parse(new File(xmlFile)); /* Contrary to the documentationm, this does not work (for JDK 1.5 and JDK 1.6)! SAXParserFactory pf = SAXParserFactory.newInstance(); pf.setValidating(false); pf.setNamespaceAware(true); pf.setSchema(schema); SAXParser p = pf.newSAXParser(); p.parse(new File(xmlFile), new org.xml.sax.helpers.DefaultHandler()); */ } }