1

I've been looking for an example on how to validate an XML file against an XSD schema using JDOM 2.0 before parsing it.

I've been searching for about an hour but wasn't able to find a solution which would work and allow me to provide the path to the XSD file (located on the local file-system) from inside the application code, rather than as an attribute in the XML file.

I am surprised that there is not an easily reachable sample snippet on the Internet already. I would be grateful if you could provide one here.

reevesy
  • 3,452
  • 1
  • 26
  • 23
Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65

2 Answers2

2

It's very easy to do with JDOM 2. You can do it with 2 lines of code :

SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
Document document = builder.build(new File("yourXmlFile.xml"));
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
jdomuser
  • 21
  • 3
  • 3
    Welcome to Stack Overflow! Please note that bare links to your own website/product are not encouraged here for two reasons; First, an answer should be posted as a self-contained answer, not a mere link to an external site. Second, self-promotion tends to be frowned upon here, and often is flagged as spam (especially if there is no disclosure that you are linking to your own site/product). – Andrew Barber Feb 08 '13 at 00:24
  • does JDOM2 support XSD 1.1? (specifically `xs:assert`) – ycomp Nov 17 '15 at 04:29
1

Try looking at the XSDSchemaFactory:

http://www.jdom.org/docs/apidocs/org/jdom2/input/sax/XMLReaderXSDFactory.html

For a more general discussion of this look at the package documentation:

http://www.jdom.org/docs/apidocs/org/jdom2/input/sax/package-summary.html

and for some information on the motivation/feature, see the wiki page:

https://github.com/hunterhacker/jdom/wiki/JDOM2-Feature-SAX-Parsing-Updates

If you have more questions, comment here, or drop a note to the jdom-interest mailing list. If you feel the documentation can be improved, please comment on that too.

rolfl
  • 17,539
  • 7
  • 42
  • 76