And here is the ObXTW (the Obligatory XML::Twig Way), once you've fixed your XML by wrapping everything into a single element:
#!/bin/perl -w
use strict;
use XML::Twig;
my $t= new XML::Twig( twig_handlers => { post => \&post });
$t->parse( \*DATA);
sub post
{ my( $t, $post)= @_; # all handlers get called with those arguments
# here is the magic!
# gi is the element name and text is its... text!
my %hash= map { $_->gi, $_->text} $post->children;
# or whatever you want to do with the hash
print "City: $hash{location} \n";
# if your file is small enough you don't need to purge, otherwise
# it will free the memory used so far
$t->purge;
}
__DATA__
<posts>
<post>
<jobnumber>1234</jobnumber>
<location> somecity NJ </location>
</post>
<post>
<jobnumber>87922</jobnumber>
<location> Othercity, AK </location>
</post>
</posts>
-
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.
|