xml parsing and validation after catching Exception
I have an xml like this which is a valid xml. Now if i parse through it using SaxParser, it validates perfectly.
<A>
<B>
<C>
<D/>
</C>
<C>
<D/>
</c>
</B>
</A>
开发者_JS百科Consider next xml which is not well structured and not valid ( < /C> is missing).It will throw SaxParserException at that point. But i need to write a code which catches this exception AND continues validating the next set of tags in this xml.
<A>
<B>
<C>
<D/>
<C>
<D/>
</c>
</B>
</A>
Do anyone know how to continue validating the xml from the point where it caught exception
Usually it's not possible and shouldn't be possible. At you second example, we have no idea, what's actually missing: is it one end tag (1) after the first <C>
or (2) after the second <C>
, do we have (3) too many opening <C>
tags, should one of the <C>
tags actually read <C />
? Far too many ways to correct the document structure.
Anyway, there is a project called xmlunit on sourceforge which contains a TolerantSaxDocumentBuilder
that claims to be able to handle missing start and end tags. It may already solve your actual problem or. At least it points in the right direction: you need a custom sax parser that implements the required behaviour.
精彩评论