Reputation: 137
I am currently trying to parse an XML file in Java. I have done lots of research and tried using many of the available libraries such as DOM, SAX, etc. All of these are great and have lots of options. My issue though is I want to be able to parse an entire XML file then output any errors that may be there. With SAX for example, it will parse the file, but only output the first syntax error. Is there anyway to output all errors at once like a compiler can or am I out of luck?
If this is not possible, I am attempting a work around to parse my XML file like a normal text file and grab all the opening and closing tags. Is there a simpler way to do this besides creating many conditional statements and rules?
Any help is greatly appreciated.
Thanks
Upvotes: 3
Views: 2347
Reputation: 15953
Just use a custom ErrorHandler
(as the default ErrorHandler
throws exception on first error).
public void validateXML(SAXSource source, Schema schema) throws SAXException, IOException
{
javax.xml.validation.Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler()
{
@Override
public void warning(SAXParseException exception) throws SAXException
{
System.err.println("Notice: " + exception.toString());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException
{
error(exception);
}
@Override
public void error(SAXParseException exception) throws SAXException
{
System.err.println("Error occured: " + exception.toString());
}
});
validator.validate(source);
}
Upvotes: 2
Reputation: 3377
I don't think you will have much success finding such type of XML parsers. By definition, an XML parser should throw an exception on first error it encounters. If it doesn't, it will have no way to identify the node type or correctness of any subsequent text ...
Upvotes: -1