Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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.

andy.

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2020-05-27 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (154 votes). Check out past polls.

    Notices?