Pages

So sánh XMLReader vs SAX parser

XMLReader vs SAX parser Recently, I tried to port some Java code to C#. Most Java code use SAX to process XML. .net framework library does not support SAX. It supports XMLReader instead. I tried to compare both approaches to see which one would result in cleaner implementation. I encountered two articles. The first article is on xml.com. The article gave a nice example on how to port SAX code to XMLReader code. The second article is by Dino Esposito on zdnet. The article favors the .net approach considering Dino is largely a Microsoft guy The difference is tat SAX use a push model while XMLReader uses a pull model. With SAX, we just need to set up an event handler. The default SAX parser acts as a driver to parse the document and fire the events. With XMLReader, we have to create a loop that keeps calling the read method of the XMLReader class. We then have to implement a switch construction that dispatch the processing to other procedures depending on the node type. This model is similar to how we process the Windows message loop. It is possible to construct the dispatching mechanism to be like that of SAX. At the first glance, it takes less code to setup SAX parser. However, in the subsequent processing, we can call one of the readxxx method of XMLReader to pull additional information. This would gives us more control and cleaner implementation than the SAX model.

Không có nhận xét nào:

Đăng nhận xét

Power by Doanh Vũ | Site Map | RSS Feed