How to Build a Simple XML Parser

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
Use XML : : Parser;
My $parse= new XML : : Parser();
$parse -> setHandlers ( Start => \&handler-start,
End => \&handler-end, );
$parse->parsefiles ($file);
sub handler-start
(
my ( $parser, $element, %attr) =@-;
print “start: $element\n”;
)
sub handler-end
(
my ($parser, $element) =@-;
print “End; $element\n”;
)
Running this on a simple XML document results in the following output :
$ perl exxmlp.pl simple.xml
Start: simple
Start: paragraph
End: paragraph
End: simple

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
<video>
<main>
<title>Alien Resurrection</title>
<para>Sigourney Weaver, Winona Ryder</para>
<title>Witness the Resurrection</title>
<para>The review..</para>
</main>
<panel>

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
Use XML : : Parser;
My $parse= new XML : : Parser();
$parse -> setHandlers ( Start => \&handler-start,
End => \&handler-end, );
$parse->parsefiles ($file);
sub handler-start
(
my ( $paser, $element, %attr) =@-;
print “start: $element\n”;
)
sub handler-end
(
my ($parser, $element) =@-;
print “End; $element\n”;
)
Running this on a simple XML document results in the following output :
$ perl exxmlp.pl simple.xml
Start: simple
Start: paragraph
End: paragraph
End: simple

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
<video>
<main>
<title>Alien Resurrection</title>
<para>Sigourney Weaver, Winona Ryder</para>
<title>Witness the Resurrection</title>
<para>The review..</para>
</main>
<panel>


.

SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.