What Is a Sequential Access Event-Driven XML Parser?


The topic of sequential access parsers is one of the most interesting concepts in the world of XML parsing. Because XML data is structured and appears in a particular order, these parsers enable us to access this data in the same order or sequence that was defined by the original XML data developer.

Most of the sequential parsers today are event-driven, and that is probably why confusion exists between the two concepts. A sequential access parser can be event-driven, but it doesn't have to be because it can conform to another model. In the event-driven model, when the sequential parser returns the chunks of XML data to the program, this data is passed to a user -defined function, which then processes the data. This function is called a callback function. A user can define a callback for each event (for example, each open tag, close tag, text, and so forth), and the parser will trigger the correct event when that construct (event) is encountered . Usually, data associated with the construct (for example, the element name ) is passed into the callback function as an argument.

A sequential access event-driven XML parser starts at the top of the XML data and steps through the data line by line. For example, let's say that we have to parse the following XML data:

 <?xml version="1.0"?>  <employees>     <home_addresses>        <name>Joseph Burns</name>        <street>123 Main Street</street>        <city>New York</city>        <state>NY</state>        <zip>10452</zip>      </home_addresses>  </employees> 

If we define a callback function that is called when an open tag for an element is encountered, then it would be called in the following order, once for each element: <employees> , <home_addresses> , <name> , <street> , <city> , <state> , and <zip> . Because the callback functions are called for each element in the same order as the elements appear in the XML data, this enables you to easily develop a Perl program to parse the XML data. It eliminates all the logic in your application that would be required if you didn't know the structure of the XML data.

These parsers are very fast and efficient because they simply read XML and return data to the program in chunks. The application then decides what to do with these chunks of data ”process them or discard them ”and enables the parser to proceed. These actions are performed until all the XML data is read, or until the program exits (due to an error or a user-defined command).

Now that we've discussed some of the theory behind XML parsers, let's take a look at some examples.



XML and Perl
XML and Perl
ISBN: 0735712891
EAN: 2147483647
Year: 2002
Pages: 145

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net