• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML validation question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. I set a boolean variable in my ErrorHandler. Thank you for your help.

Ben
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic