tags:

views:

195

answers:

2

I'm relatively new to working with XML and am working with some rather large documents using the javax.xml.parsers.SAXParser .

The thing is: The information I need is near the top of the XML file and I don't need to parse the entire document. -- What is the "best" way to stop parsing a file when the relevant information has been identified?

(So far the only way I can think of is to throw an exception.)

+2  A: 

You can use StAX. It actually stops the parsing process any time without throwing any exception.

Check out:

http ://www.xml.com/pub/a/2003/09/17/stax.html

http ://jcp.org/en/jsr/detail?id=173

Green Techy
This is the best way since throwing an exception to control the normal execution flow is not so elegant(except if there is no other way to do it, but since it is it should be avoided)."Streaming pull parsing(StAX) refers to a programming model in which a client application calls methods on an XML parsing library when it needs to interact with an XML infoset.Streaming push parsing(SAX) refers to a programming model in which an XML parser sends (pushes) XML data to the client as the parser encounters elements in an XML."http://java.sun.com/webservices/docs/1.6/tutorial/doc/SJSXP2.html
adrian.tarau
+2  A: 

Throwing an exception is the only way to stop it. See this IBM XML tip for an example.

You should probably implement your own exception to signal an intention to stop further processing. That way you will be able to distinguish between an intentional halt to processing, and an unintentional halt (when encountering some unexpected scenario etc.)

Brian Agnew