0

I want to skip the node which contains an error. I use SAXParser

Example XML:

<file>
<person>
<id>1
<name>Jhon</name>
</person>
<person>
<id>2</id>
<name>Julia</name>
</person>
</file>

I use:

SAXParserFactory fact= SAXParserFactory.newInstance();
SAXParser parser= fact.newSAXParser();
MyHandler handler = new MyHandler ();
parser.parse(new File(path), handler);

Example of handler :

public class MyHandler extends org.xml.sax.helpers.DefaultHandler
{

    private String message = "";

    @Override
    public void fatalError(final SAXParseException e)
    {
        message += "Error : " + e.getMessage();
    }
}

I want to skip the error of person with id 1 because we don't have </id> and continue the execution to person 2 and just save the error message.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Sax won't parse not well formed XML, you will have to fix it first – LMC May 10 '22 at 14:42
  • Does this answer your question? [How to parse invalid (bad / not well-formed) XML?](https://stackoverflow.com/questions/44765194/how-to-parse-invalid-bad-not-well-formed-xml) – Joe May 11 '22 at 08:45

1 Answers1

0

There are parsers such as TagSoup and validator.nu that attempt to parse bad XML. Whether they succeed depends on just how bad the XML is.

And of course they have to guess what the "correct" XML was meant to be. In your example, the XML can be made well-formed by adding an </id> end tag anywhere before the </person> tag, so the repair may not be the one you would have liked.

You say you want to skip invalid records, but I think the philosophy of these products is to try to repair them rather than skipping them.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164