How to Build a Simple XML ParserXml: : Parser is built on top of the Expat XML processing library written by James Clark XML : : parser is a vital component of XML processing under Perl because most other modules within Perl use the facilities offered by XML : : Parser to support their own processing.
The interface to the parser is simple; You create a new XML : : Parser object, a suite of functions that are called when the parser determines a start, end, or data protion in your XML document. For example, the code in Listing 14.1 builds a very simple XML parser to output the start and end tags in a document. Listing 14.1: A Simple XML Parser As you can , the sample outputs a list of the start and end tags.Because we “register “ the fungtions that we want to call when different elements are seen, the functions can be called anything we like. Note as well that the sfunctions are supplied with the name of the tag that was found and the list of attributes for a given tag. We can use this information within the parsing process to be more explicit about the information we pass on. Using XML : : Parser to Convert to HTML Being an event-based parser, the XML : : Parser module is ideal in situations where you need to extract or convert those elements into anotherform . Converting an XML document into an HTML format for display on screen is a good example. We”re going to be looking at a CGI script that I wrote on behalf of a client who wanted tto convert an XML document into HTML for displaying on its Web site. The documents themselves were a mixture of XML and some HTML components, and you can see a sample in Listing 14.2 Listing 14.2: A Sample Review Document Xml: : Parser is built on top of the Expat XML processing library written by James Clark XML : : parser is a vital component of XML processing under Perl because most other modules within Perl use the facilities offered by XML : : Parser to support their own processing. XML : : Parser itself is an event-based parser, and because it uses the Expat libraries, it also offers simple validation of your XML documents for well-formedness, although it doesn’t validate your documents against a DTD. The interface to the parser is simple; You create a new XML : : Parser object, a suite of functions that are called when the parser determines a start, end, or data protion in your XML document. For example, the code in Listing 14.1 builds a very simple XML parser to output the start and end tags in a document. Listing 14.1: A Simple XML Parser As you can , the sample outputs a list of the start and end tags.Because we “register “ the fungtions that we want to call when different elements are seen, the functions can be called anything we like. Note as well that the sfunctions are supplied with the name of the tag that was found and the list of attributes for a given tag. We can use this information within the parsing process to be more explicit about the information we pass on. Using XML : : Parser to Convert to HTML Being an event-based parser, the XML : : Parser module is ideal in situations where you need to extract or convert those elements into anotherform . Converting an XML document into an HTML format for display on screen is a good example. We”re going to be looking at a CGI script that I wrote on behalf of a client who wanted tto convert an XML document into HTML for displaying on its Web site. The documents themselves were a mixture of XML and some HTML components, and you can see a sample in Listing 14.2 Listing 14.2: A Sample Review Document
|
|
|||
|
||||
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”
Copyright - © 2004 - 2019 - All Rights Reserved.