Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML syntax validation in Java [closed]

I've been trying to figure out how to check the syntax of an XML file, make sure all tags are closed, there's no random characters, etc... All I care at this point is making sure there is no broken XML in the file.

I've been looking at some SO posts like these...

  • Validate an XML file against local DTD file with Java

  • What's the best way to validate an XML file against an XSD file?

... but I realized that I don't want to validate the structure of the XML file; I don't want to validate against an XML Schema (XSD)... I just want to check the XML syntax and determine if it is correct.

like image 642
Hristo Avatar asked Jun 15 '11 19:06

Hristo


People also ask

Is XML valid Java?

Java XML Validation API can be used to validate XML against XSD in java program. javax. xml. validation.

Does XML allows validation?

XML validation also strips off ignorable whitespace in the XML document. Validation is optional but highly recommended when data integrity is in question, since it ensures that XML documents abide by the rules provided by their XML schemas on top of being well-formed.


2 Answers

You can check if an XML document is well-formed using the following code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true);  DocumentBuilder builder = factory.newDocumentBuilder();  builder.setErrorHandler(new SimpleErrorHandler());     // the "parse" method also validates XML, will throw an exception if misformatted Document document = builder.parse(new InputSource("document.xml")); 

The SimpleErrorHandler class referred to in the above code is as follows:

public class SimpleErrorHandler implements ErrorHandler {     public void warning(SAXParseException e) throws SAXException {         System.out.println(e.getMessage());     }      public void error(SAXParseException e) throws SAXException {         System.out.println(e.getMessage());     }      public void fatalError(SAXParseException e) throws SAXException {         System.out.println(e.getMessage());     } } 

This came from this website, which provides various methods for validating XML with Java. Note also that this method loads an entire DOM tree into memory, see comments for alternatives if you want to save on RAM.

like image 120
James Allardice Avatar answered Sep 27 '22 18:09

James Allardice


What you are asking is how to verify that a piece of content is well-formed XML document. This is easily done by simply letting an XML parser (try to) parse content in question -- if there are issues, parser will report an error by throwing exception. There really isn't anything more to that; so all you need is to figure out how to parse an XML document.

About the only thing to beware is that some libs that claim to be XML parsers are not really proper parsers, in that they actually might not verify things that XML parser must do (as per XML specification) -- in Java, Javolution is an example of something that does little to no checking; VTD-XML and XPP3 do some verification (but not all required checks). And at the other end of spectrum, Xerces and Woodstox check everything that specification mandates. Xerces is bundled with JDK; and most web service frameworks bundle Woodstox in addition.

Since the accepted answer already shows how to parse content into a DOM document (which starts with parsing), that might be enough. The only caveat is that this requires that you have 3-5x as much memory available as raw size of the input document. To get around this limitation you could use a streaming parser, such as Woodstox (which implements standard Stax API). If so, you would create an XMLStreamReader, and just call reader.next() as long as reader.hasNext() returns true.

like image 45
StaxMan Avatar answered Sep 27 '22 18:09

StaxMan