System.Xml contains many very useful classes. When you’re talking about stream-based XML parsing, however, XmlReader is what you want..
XmlReader is most analogous to SAX, although it does not require implementing an interface as most SAX implementations do (as JAXP’s does). Instead, you simply instantiate a concrete XmlReader of your choice — there are several to choose from: XmlTextReader and XmlStreamReader are the most useful — then call its Read() method and pick nodes off as they are returned to you.
In algorithmic terms, we could say that SAX uses callbacks, while XmlReader uses an event loop. An event loop is an infinite loop, during which certain events are received and dispatched to you. Internally, SAX may well have the same sort of event loop, but the callback methods hide that particular detail from you. A callback method is simply invoked when SAX’s parser comes across an event.
While that means that we don’t need all those callback methods to satisfy an interface, we can still use them to dig through the nodes that we’ve read. In fact, we’re just retrofitting a SAX-like API on top of XmlReader. Here’s the new version of the code; be sure and compare these to the Java version above.
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment