4

I have an xml file that looks like:

<root>
<application-key>ABC2349293493</application-key>
...
..
</root>

The xml file is rather large, and I only need to get the application-key to see if this request should be rate limited.

My server is getting pounded, so I need a way to rate limit requests without having to read the entire xml.

DOM is out of the question because of the memory footprint.

I am happy with SAX, but with xerces you are not able to stop processing of the xml since it is a push model. The only way I know so far is the throw an exception.

Do any of the other sax libraries support exiting early gracefully?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

2 Answers2

3

StAX (Streaming Api for XML) pull parser. Choosing the proper implementation is also important: Best StAX Implementation.

P.S. I still think SAX way of doing things (throwing an exception) is simpler.

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
2

An exception is indeed the only option in the spec that is available to exit the parsing process. As outlined Java. Sax parser. How to manually break parsing? or How to stop parsing xml document with SAX at any time?.

Switch to a pull parser if you REALLY don't want to use an exception, but I don't think it really matters.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Does it cause any performance issue though? that is what I am concerned about. I'm going to test it but theoretically exceptions are slow correct? – Blankman Dec 23 '11 at 21:09
  • 1
    @Blankman - "theoretically" compared to what? compared to executing a single machine instruction, maybe. compared to parsing xml, doubtful. – jtahlborn Dec 23 '11 at 21:33