The HTTP response entity body: XML and XML parser

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The content of this article

 

The HTTP response entity body: XML

Entity (entity-body) is usually the most important part of the HTTP response. The Web service, the entity body is usually a XML document, which contains most of the information needed by the client. These information, after the XML parser analysis, can be used for the client.

In URI, a group header and a main document, and then use the HTTP library to put these data into HTTP requests, and sent to the server, and then use the XML parser to parse HTTP in response to a data structure or a series of events. We follow the service request request, as for the response data returned by the service, you can according to their own wishes arbitrary use.

Although most Web service returns are XML documents, but also has a simple data structure of Web service returns as an JSON string. JSON is usually for the Ajax client application part use. The idea is: the browser to get JavaScript data structure from the JSON data structure, rather than from the XML document to get the JavaScript data structures easier. JSON provides the serialized data a lightweight scheme, as the supplement of XML scheme based on.

XML parser

XML parser can be divided into three kinds, two kinds of basic XML parsing strategy: Based on document (document-based) strategy and event based (event-based) strategy. Document based strategies, such as DOM tree parser; event based strategies, such as SAX and " drag " style (pull) parser.

The document tree based strategy, is the simplest of the three. Tree parser the XML document is modeled as a nested data structure. This data structure, once generated, you can use the XPath query, CSS selector (selector) to query and processing it. The DOM parser is a kind of tree type parser, it implements a W3C interface. The advantages of tree type strategy is simple to use. But the biggest drawback is: you must put the document as a whole to process. That is to say, you have one can imagine in the whole document completely transformed into a tree structure to it -- which requires the whole document is loaded into memory,, if large documents, even if has the advantages of simple structure, it is also very inefficient. If you can " analysis of a point, a point " good...

SAX type or towed (pull) parser to transform an XML document into a list of events, rather than the data structure. The start tag (starting tag), the end tag (closing tag), XML (comments) and annotated entity declaration (entity declaration) are all events(event).

Towed parser(pull parser). If almost every event you have to deal with, the pull parser will be very useful. Pull parser allows you to a processing an event, namely: if needed, from the event stream from the " " (pull) the next event. You can drag a " " event to make corresponding processing, also can build a data structure with it (mostly smaller than the data structure of tree type) for future use, you can pause to parse the document at any time, and need to continue, then from the event stream from the " " a events.

The SAX parser (SAX parser) is more complex, but it is in you only care about some event is very useful. You can register a callback method to SAX parser. Once defined callback method, the parser will be established according to the document, independent steps down; the parser will transform the XML document into some series of events, and after processing each event in a document, when corresponding to a single event and the callback event match, the callback method will trigger the parser, the execution of your definition of the code, the callback method after execution, the SAX parser will then continue processing sequence of events.

Summary

The document parser, based on advantages, you can randomly access the content in the document; and for the event based parser, event triggered, will have no chance of re treatment. If you want to trigger it, we'll have to parse the document.

But, if has a bad format XML document parser, event until the processing to the wrong place will be based on the error and collapse; therefore, if you don't want a callback method is has a bad format document lock trigger, must be in the XML documents to the event based parsers handle, ensure the document good structure.

The programming language XML parser

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Noel at November 15, 2013 - 1:42 AM