287

I'm generating some xml files that needs to conform to an xsd file that was given to me. How should I verify they conform?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jeff
  • 3,252
  • 3
  • 23
  • 13

13 Answers13

355

The Java runtime library supports validation. Last time I checked this was the Apache Xerces parser under the covers. You should probably use a javax.xml.validation.Validator.

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import org.xml.sax.SAXException;
//import java.io.File; // if you use File
import java.io.IOException;
...
URL schemaFile = new URL("http://host:port/filename.xsd");
// webapp example xsd: 
// URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
// local file example:
// File schemaFile = new File("/location/to/localfile.xsd"); // etc.
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
  Schema schema = schemaFactory.newSchema(schemaFile);
  Validator validator = schema.newValidator();
  validator.validate(xmlFile);
  System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
  System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {}

The schema factory constant is the string http://www.w3.org/2001/XMLSchema which defines XSDs. The above code validates a WAR deployment descriptor against the URL http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd but you could just as easily validate against a local file.

You should not use the DOMParser to validate a document (unless your goal is to create a document object model anyway). This will start creating DOM objects as it parses the document - wasteful if you aren't going to use them.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • Are you using a DOM or SAX parser in this example? How do i tell which parser you are using as i cant see a reference to either. – ziggy Jul 21 '12 at 12:15
  • 1
    @ziggy - this is an implementation detail of the [JAXP implementation](http://jaxp.java.net/). Sun's JDK 6 uses SAX parser with a [StreamSource](http://docs.oracle.com/javase/6/docs/api/javax/xml/transform/stream/StreamSource.html). A JAXP implementation _could_ legally use a DOM parser in this case, but there is no reason to. If you [use a DOM parser](http://stackoverflow.com/a/15739/304) explicitly for validation, you will definitely instantiate a DOM tree. – McDowell Jul 21 '12 at 14:56
  • How do i use an ErrorHandler with the above? Is is a case of just creating the ErrorHandler and associating it with the validator? i.e. validator.SetErrorHandler() as in the example in this SO question http://stackoverflow.com/questions/4864681/jaxb-2-0-schema-validation-problem? – ziggy Jul 22 '12 at 18:14
  • Shouldn't execptions **just** be used for execptional situations and **not** for control flow? – mike Jul 19 '13 at 14:48
  • Won't this code only catch fatal errors? If you want to be able to catch non-fatals (such as non-structural ones) I think you will need to use an ErrorHandler. – matt forsythe May 30 '14 at 21:13
  • This code doesn't work when the file to validate contains a DOCTYPE declaration, if someone know why ? – HugoPoi Dec 03 '14 at 15:55
  • If you are interested in how to validate against a set of local schemas, take a look at https://stackoverflow.com/questions/44996345/offline-xml-validation – jschnasse Jan 25 '18 at 16:20
  • It works fine, but in Sonar cube scan it will show "Disable XML external entity (XXE) processing", and it is blocker for a code – Rohit Maurya Dec 09 '20 at 06:20
25

Here's how to do it using Xerces2. A tutorial for this, here (req. signup).

Original attribution: blatantly copied from here:

import org.apache.xerces.parsers.DOMParser;
import java.io.File;
import org.w3c.dom.Document;

public class SchemaTest {
  public static void main (String args[]) {
      File docFile = new File("memory.xml");
      try {
        DOMParser parser = new DOMParser();
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setProperty(
             "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
             "memory.xsd");
        ErrorChecker errors = new ErrorChecker();
        parser.setErrorHandler(errors);
        parser.parse("memory.xml");
     } catch (Exception e) {
        System.out.print("Problem parsing the file.");
     }
  }
}
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
SCdF
  • 57,260
  • 24
  • 77
  • 113
  • 10
    The SAX parser would be more efficient - the DOM parser creates DOM objects; wasteful operations in this instance. – McDowell Sep 17 '08 at 21:02
  • The question is to validate an XML against a XSD. In this answer you are going further and getting a Parser object, which is not needed, right? – Weslor Oct 29 '15 at 13:43
  • "ErrorChecker cannor be resolved to a type" .. missing import ? – Alex Mar 11 '16 at 11:33
20

We build our project using ant, so we can use the schemavalidate task to check our config files:

<schemavalidate> 
    <fileset dir="${configdir}" includes="**/*.xml" />
</schemavalidate>

Now naughty config files will fail our build!

http://ant.apache.org/manual/Tasks/schemavalidate.html

chickeninabiscuit
  • 9,061
  • 12
  • 50
  • 56
18

Since this is a popular question, I will point out that java can also validate against "referred to" xsd's, for instance if the .xml file itself specifies XSD's in the header, using xsi:schemaLocation or xsi:noNamespaceSchemaLocation (or xsi for particular namespaces) ex:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd">
  ...

or schemaLocation (always a list of namespace to xsd mappings)

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.example.com/my_namespace http://www.example.com/document.xsd">
  ...

The other answers work here as well, because the .xsd files "map" to the namespaces declared in the .xml file, because they declare a namespace, and if matches up with the namespace in the .xml file, you're good. But sometimes it's convenient to be able to have a custom resolver...

From the javadocs: "If you create a schema without specifying a URL, file, or source, then the Java language creates one that looks in the document being validated to find the schema it should use. For example:"

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema();

and this works for multiple namespaces, etc. The problem with this approach is that the xmlsns:xsi is probably a network location, so it'll by default go out and hit the network with each and every validation, not always optimal.

Here's an example that validates an XML file against any XSD's it references (even if it has to pull them from the network):

  public static void verifyValidatesInternalXsd(String filename) throws Exception {
    InputStream xmlStream = new new FileInputStream(filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                 "http://www.w3.org/2001/XMLSchema");
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new RaiseOnErrorHandler());
    builder.parse(new InputSource(xmlStream));
    xmlStream.close();
  }

  public static class RaiseOnErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
    public void error(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
    public void fatalError(SAXParseException e) throws SAXException {
      throw new RuntimeException(e);
    }
  }

You can avoid pulling referenced XSD's from the network, even though the xml files reference url's, by specifying the xsd manually (see some other answers here) or by using an "XML catalog" style resolver. Spring apparently also can intercept the URL requests to serve local files for validations. Or you can set your own via setResourceResolver, ex:

Source xmlFile = new StreamSource(xmlFileLocation);
SchemaFactory schemaFactory = SchemaFactory
                                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema();
Validator validator = schema.newValidator();
validator.setResourceResolver(new LSResourceResolver() {
  @Override
  public LSInput resolveResource(String type, String namespaceURI,
                                 String publicId, String systemId, String baseURI) {
    InputSource is = new InputSource(
                           getClass().getResourceAsStream(
                          "some_local_file_in_the_jar.xsd"));
                          // or lookup by URI, etc...
    return new Input(is); // for class Input see 
                          // https://stackoverflow.com/a/2342859/32453
  }
});
validator.validate(xmlFile);

See also here for another tutorial.

I believe the default is to use DOM parsing, you can do something similar with SAX parser that is validating as well saxReader.setEntityResolver(your_resolver_here);

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • Doesn't work for me, method resolveResource() isn't called unless its set on schemaFactory, any idea? – tomasb Jul 25 '18 at 14:21
  • Dunno, works for me. Make sure you're setting it via `setResourceResolver` but beyond that, maybe open new question... – rogerdpack Jul 25 '18 at 15:55
  • 1
    Resurrecting an old post, I think it should read `xsi:schemaLocation` instead of `xsi:SchemaLocation` - case matters. See https://www.w3.org/TR/xmlschema-1/#d0e3067 – Christian Schlichtherle Sep 09 '20 at 18:14
6

Using Java 7 you can follow the documentation provided in package description.

// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(new File("mySchema.xsd"));
Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree
try {
    validator.validate(new StreamSource(new File("instance.xml"));
} catch (SAXException e) {
    // instance document is invalid!
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • 2
    *"Using Java 7.."* That was actually included in [**Java 5**](http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html). – Andrew Thompson Aug 20 '13 at 21:05
  • 4
    This is basically the same as [the accepted answer](http://stackoverflow.com/a/16054). This solution seems to me a bit inefficient though, as it unnecessarily builds the DOM for the xml to parse: `parser.parse(new File("instance.xml"))`. The `validator` accepts a `Source`, so you can: `validator.validate(new StreamSource(new File("instance.xml")))`. – Alberto Jul 17 '14 at 04:55
  • Working this way, a SAXException would be thrown at the first error in the xml-file and stops then the validation. But I want to know all (!) errors. If I use an ErrorHandler (own class that implements ErrorHandler) instead, it recognizes all errors, but the try-catch-block of validator.validate does not throw any Exception.. How do I recognize an error in the class that invokes the validate-method of my validator? Thanks for your help! – mrbela Jan 13 '15 at 10:44
  • There are "errors" (e.g. validation errors) and "fatal errors" (well-formedness errors). One fatal error typically stops the parsing. But a validation error does not stop it : you have to explicitly throw an exception. Thus, it is necessary to provide an `ErrorHandler` if you need to do validation. – Ludovic Kuty Oct 22 '17 at 06:04
  • 1
    Gotta admit, the code looks cleaner and easier to read on this than the accepted answer. – Clockwork Jan 17 '19 at 16:22
  • 2
    The validate line lacks a closing parenthesis. – ceving Aug 17 '20 at 11:57
3

With JAXB, you could use the code below:

    @Test
public void testCheckXmlIsValidAgainstSchema() {
    logger.info("Validating an XML file against the latest schema...");

    MyValidationEventCollector vec = new MyValidationEventCollector();

    validateXmlAgainstSchema(vec, inputXmlFileName, inputXmlSchemaName, inputXmlRootClass);

    assertThat(vec.getValidationErrors().isEmpty(), is(expectedValidationResult));
}

private void validateXmlAgainstSchema(final MyValidationEventCollector vec, final String xmlFileName, final String xsdSchemaName, final Class<?> rootClass) {
    try (InputStream xmlFileIs = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlFileName);) {
        final JAXBContext jContext = JAXBContext.newInstance(rootClass);
        // Unmarshal the data from InputStream
        final Unmarshaller unmarshaller = jContext.createUnmarshaller();

        final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final InputStream schemaAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xsdSchemaName);
        unmarshaller.setSchema(sf.newSchema(new StreamSource(schemaAsStream)));

        unmarshaller.setEventHandler(vec);

        unmarshaller.unmarshal(new StreamSource(xmlFileIs), rootClass).getValue(); // The Document class is the root object in the XML file you want to validate

        for (String validationError : vec.getValidationErrors()) {
            logger.trace(validationError);
        }
    } catch (final Exception e) {
        logger.error("The validation of the XML file " + xmlFileName + " failed: ", e);
    }
}

class MyValidationEventCollector implements ValidationEventHandler {
    private final List<String> validationErrors;

    public MyValidationEventCollector() {
        validationErrors = new ArrayList<>();
    }

    public List<String> getValidationErrors() {
        return Collections.unmodifiableList(validationErrors);
    }

    @Override
    public boolean handleEvent(final ValidationEvent event) {
        String pattern = "line {0}, column {1}, error message {2}";
        String errorMessage = MessageFormat.format(pattern, event.getLocator().getLineNumber(), event.getLocator().getColumnNumber(),
                event.getMessage());
        if (event.getSeverity() == ValidationEvent.FATAL_ERROR) {
            validationErrors.add(errorMessage);
        }
        return true; // you collect the validation errors in a List and handle them later
    }
}
razvanone
  • 1,351
  • 18
  • 27
3

One more answer: since you said you need to validate files you are generating (writing), you might want to validate content while you are writing, instead of first writing, then reading back for validation. You can probably do that with JDK API for Xml validation, if you use SAX-based writer: if so, just link in validator by calling 'Validator.validate(source, result)', where source comes from your writer, and result is where output needs to go.

Alternatively if you use Stax for writing content (or a library that uses or can use stax), Woodstox can also directly support validation when using XMLStreamWriter. Here's a blog entry showing how that is done:

Jens Møller
  • 525
  • 5
  • 20
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Hey StaxMan, are there any XMLStreamWriters that do pretty-print indenting? I was surprised that it's not in the standard implementation. Also, is it getting much use? I think it's the right way to go, but there seems very little interest in it. – 13ren Mar 28 '09 at 08:31
  • just found your post here about StaxMate (but it's not an XMLStreamWriter): http://stackoverflow.com/questions/290326/stax-xml-formatting-in-java/485126#485126 – 13ren Mar 28 '09 at 08:47
  • Yeah, StaxMate can do that. It uses XMLStreamWriter internally for writing content, so you can hook up validator that way too. – StaxMan Apr 01 '10 at 05:56
3

If you have a Linux-Machine you could use the free command-line tool SAXCount. I found this very usefull.

SAXCount -f -s -n my.xml

It validates against dtd and xsd. 5s for a 50MB file.

In debian squeeze it is located in the package "libxerces-c-samples".

The definition of the dtd and xsd has to be in the xml! You can't config them separately.

juwens
  • 3,729
  • 4
  • 31
  • 39
2

If you are generating XML files programatically, you may want to look at the XMLBeans library. Using a command line tool, XMLBeans will automatically generate and package up a set of Java objects based on an XSD. You can then use these objects to build an XML document based on this schema.

It has built-in support for schema validation, and can convert Java objects to an XML document and vice-versa.

Castor and JAXB are other Java libraries that serve a similar purpose to XMLBeans.

Todd
  • 1,906
  • 2
  • 16
  • 21
1

Using Woodstox, configure the StAX parser to validate against your schema and parse the XML.

If exceptions are caught the XML is not valid, otherwise it is valid:

// create the XSD schema from your schema file
XMLValidationSchemaFactory schemaFactory = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
XMLValidationSchema validationSchema = schemaFactory.createSchema(schemaInputStream);

// create the XML reader for your XML file
WstxInputFactory inputFactory = new WstxInputFactory();
XMLStreamReader2 xmlReader = (XMLStreamReader2) inputFactory.createXMLStreamReader(xmlInputStream);

try {
    // configure the reader to validate against the schema
    xmlReader.validateAgainst(validationSchema);

    // parse the XML
    while (xmlReader.hasNext()) {
        xmlReader.next();
    }

    // no exceptions, the XML is valid

} catch (XMLStreamException e) {

    // exceptions, the XML is not valid

} finally {
    xmlReader.close();
}

Note: If you need to validate multiple files, you should try to reuse your XMLInputFactory and XMLValidationSchema in order to maximize the performance.

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
0

Are you looking for a tool or a library?

As far as libraries goes, pretty much the de-facto standard is Xerces2 which has both C++ and Java versions.

Be fore warned though, it is a heavy weight solution. But then again, validating XML against XSD files is a rather heavy weight problem.

As for a tool to do this for you, XMLFox seems to be a decent freeware solution, but not having used it personally I can't say for sure.

Adam
  • 25,966
  • 23
  • 76
  • 87
0

Validate against online schemas

Source xmlFile = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("your.xml"));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(Thread.currentThread().getContextClassLoader().getResource("your.xsd"));
Validator validator = schema.newValidator();
validator.validate(xmlFile);

Validate against local schemas

Offline XML Validation with Java

jschnasse
  • 8,526
  • 6
  • 32
  • 72
-3

I had to validate an XML against XSD just one time, so I tried XMLFox. I found it to be very confusing and weird. The help instructions didn't seem to match the interface.

I ended up using LiquidXML Studio 2008 (v6) which was much easier to use and more immediately familiar (the UI is very similar to Visual Basic 2008 Express, which I use frequently). The drawback: the validation capability is not in the free version, so I had to use the 30 day trial.

KnomDeGuerre
  • 245
  • 1
  • 3
  • 8