• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

XML validation question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am validating an XML file against a schema. I have an ErrorHandler handling the error messages and logging the errors. What I am wondering is, after the validation is done and the errors are printed, how do I know whether the document is valid? Here is my code:

DocumentBuilder builder = factory.newDocumentBuilder();

builder.setErrorHandler(new SimpleErrorHandler());

Document document = builder.parse(new InputSource(new StringReader(modifyXML)));

//check to see whether validation was successful


public class SimpleErrorHandler implements ErrorHandler {
private Logger logger = Logger
.getLogger("com.bwi.oddr.activity.ValidateMessageActivity");

public void warning(SAXParseException e) throws SAXException {
logger.warn(e.getMessage());
}

public void error(SAXParseException e) throws SAXException {
logger.error(e.getMessage());
}

public void fatalError(SAXParseException e) throws SAXException {
logger.fatal(e.getMessage());
}
}

It looks like an empty document is passed back from the parse method even if the validation failed. So what is the best way to check the Document to see if the validation was successful or not?
 
Marshal
Posts: 25427
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me start by rejecting the question. You have chosen the route of checking the document to see if it's valid without examining the possibility of simpler tests -- like setting a boolean variable in your ErrorHandler for example.
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I set a boolean variable in my ErrorHandler. Thank you for your help.

Ben
 
I am going to test your electrical conductivity with this tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic