Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

andye Re: Pasring XML into a simple hash

by andye (Curate)
on Jun 21, 2001 at 15:47 UTC ( #90333=note: print w/replies, xml ) Need Help??

in reply to Pasring XML into a simple hash

For a quick-and-dirty regexp solution, how about this...
while ($text =~ m|<jobnumber>(.*?)</jobnumber>.*?<location>(.*?)</loc +ation>|sg) { print "Found job number $1 in location $2 \n"; }
NB I'm assuming the missing slashes in the data are a typo. If not then it's easy enough to modify the above.

Of course, a regexp solution isn't the right one if you want it to work in more general cases - like if the tags are the other way round, or whatever. If there's going to be any variation in the data, then the way to go is an XML parser as described by others above.


Some Time Later: Just For Fun, I tried to see if I could write one that /would/ work with the tags either way round... came up with this...

my $regexp = '<post>'; $regexp .= "(?=.*?<$_>(.*?)</$_>)" foreach qw(jobnumber location); $regexp .= '.*?</post>'; while ($stuff =~ m|$regexp|sog) { print "Found job number $1 in location $2 \n"; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://90333]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-01-28 07:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found