Almost certainly you don't want to parse XML using hand rolled code. Instead use one of the many XML parsing modules (XML::Twig is highly recommended). Robustly parsing XML is hard and you will spend much more time trying to get it right than you will learning to use a module to do the heavy lifting for you. Consider:
use warnings;
use strict;
use XML::Twig;
my $xml = <<XML;
<ROOT hostname="bumblebee" tstamp="2011/09/21 22:24:05">
<APPLICATION>
<PORT>7777</PORT>
<APP_HOME>/extra/localcw/opt/APP/sun4</APP_HOME>
<VERSION>V36.11.01</VERSION>
<PERF_HOME>/usr/localcw/opt/APP/Solaris-2-9-sparc-64</
+PERF_HOME>
<PERF_VERSION>glanceSunOS 5.9 (Solaris 9) (sparc, 64 B
+it) 7.3.00.6059 Jul 19 2006</PERF_VERSION>
<STAR_VERSION>3.0</STAR_VERSION>
<DEFAULT_ACCT>root</DEFAULT_ACCT>
<HISTORY_RETENTION>90</HISTORY_RETENTION>
<LAST_FILE_DOWN>StAR-201105090928.tar</LAST_FILE_DOWN>
<LAST_STATUS>No download file found</LAST_STATUS>
<ACL>
<ACCOUNT id="f9a64ef61c">
<MD5>f9a64ef61c</MD5>
<USERNAME>*</USERNAME>
<HOST>flower</HOST>
<PERMISSION>P</PERMISSION>
</ACCOUNT>
</ACL>
</APPLICATION>
</ROOT>
XML
my $twig = XML::Twig->new(
twig_roots => {'APPLICATION' => \&doStuff, 'ACL' => \&doStuff}
);
$twig->parse($xml);
sub doStuff {
my ($t, $elt) = @_;
print "Found ", $elt->tag(), "\n";
$t->purge; # frees the memory
}
Prints:
Found ACL
Found APPLICATION
True laziness is hard work
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|