how to stop processing xml file when error found

J

John_Woo

Hi,

I'm using apache DefaultHandler SAX to interpret xml file.

supposed the xml file as:

<a>....</a>
<a>...</a>
<a>...</a>
....
<a>...</a>

and the dtd as

<!ELEMENT a EMPTY>

namely not allowing PCDATA:

</a>no letter here<a></a>

there are some methods in this handler, such as

public void startElement()
public void endElement( String namespaceURI,
String localName, String qName ) throws SAXException
public void error(SAXParseException e) throws SAXException {
}
...

for every <a></a>, all above methods involved to process them. but the
hanlders reports the error

org.xml.sax.SAXParseException: The content of element type "a" must
match....

at the end of processing xml file.

I'm wondering,

1. why it reports the error that late?
2. how to stop processing xml file whenever error found?
 
O

Oliver Wong

John_Woo said:
Hi,

I'm using apache DefaultHandler SAX to interpret xml file.

supposed the xml file as:

<a>....</a>
<a>...</a>
<a>...</a>
...
<a>...</a>

and the dtd as

<!ELEMENT a EMPTY>

namely not allowing PCDATA:

</a>no letter here<a></a>

there are some methods in this handler, such as

public void startElement()
public void endElement( String namespaceURI,
String localName, String qName ) throws SAXException
public void error(SAXParseException e) throws SAXException {
}
..

for every <a></a>, all above methods involved to process them. but the
hanlders reports the error

org.xml.sax.SAXParseException: The content of element type "a" must
match....

at the end of processing xml file.

I'm wondering,

1. why it reports the error that late?

Probably your SAX parser is first checking that the XML is well formed,
and then only afterwards checking if it is valid. This is generally
reasonable behaviour, because there's no point in checking for validity if
the XML is not even wellformed. An example of a not well formed XML
document:

<xml>
<<><><><><<><><>>><><
2. how to stop processing xml file whenever error found?

Code the intelligence for error detection in your SAX event handling
methods.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top