Re: validating XML without throwing a Java exception

Rolf Lear <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hi Cliff.

You can 'catch' the Exception for each string that you validate.

For example

SAXBuilder builder = new SAXBuilder();
for (String maybexml : stringstotest) {
  try {
    StringReader reader = new StringReader(maybexml);
    Document doc = builder.build(reader);
    ... do something with the well-formed document....
  } catch (JDOMException jdome) {
    // That string was not valid XML
    // do something about it....

    System.out.println("String " + maybexml + " was not XML: " +
jdome.getMessage());

  } catch (IOEXception ioe) {
    // StringReader should never throw an IOException.... but, just in
case
    ioe.printStackTrace();
  }
}


Rolf


On Mon, 7 Nov 2011 10:42:35 -0500, cliff palmer <[email protected]>
wrote:
> I need to examine several hundred thousand text strings and accumulate a
> count of the number of strings containing "well-formed XML" (i.e. can be
> parsed with saxBuilder and then used) vs "poorly-formed" (i.e. something
in
> the string prevents successful parsing).  My (limited) experience is
that
> an exception (such as a JDOMException thrown by Saxbuilder.build()) will
> halt execution.
> How can I validate the XML in these strings without halting execution?
> Thanks!
> Cliff
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.