[jdom-interest] validating XML without throwing a Java exception

Rolf Lear jdom at tuis.net
Mon Nov 7 07:59:46 PST 2011

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: " +

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


On Mon, 7 Nov 2011 10:42:35 -0500, cliff palmer <palmercliff at gmail.com>
> 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
> the string prevents successful parsing).  My (limited) experience is
> 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

More information about the jdom-interest mailing list