Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Parsing XML into a simple hash

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

in reply to Pasring XML into a simple hash

If you don't have strict XML (i.e. no ending /location) tag why not just use regular expressions? This seems to work..
#!/usr/bin/perl use strict; open (IN,"testxml.dat"); my @buf = <IN>; close IN; for (my $i=0; $i<=$#buf; $i++) { if ($buf[$i] =~ s/^\s*<jobnumber>(.*)<\/jobnumber>\s*$/$1/) { $buf[$i+1] =~ s/^\s*<location>\s*(.*)\s<location>\s*$/$1/; # if your tags are really like this &process($buf[$i],$buf[$i+1]); } } sub process { my ($jobnumber,$location) = @_; print "Found a job $jobnumber in $location.\n"; # do something }
On a related note, I tried to lose the spaces inside the location tags and couldn't get this kind of regex to work, anyone?
$buf[$i+1] =~ s/^\s*<location>\s*(.?)\s*<location>\s*$/$1/; # \s?(.*)\s works though..

Replies are listed 'Best First'.
(Ovid - don't use regexes for parsing) Re(2): Parsing XML
by Ovid (Cardinal) on Jun 21, 2001 at 22:19 UTC

    Your solution does not work. Initially, it will appear to work against his data set, but XML start and end tags don't have to appear on the same line. If that happens, your regex will break because the dot metacharacter doens't match the newline. Adding the /s modifier allows the dot to match, but then, because your match is greedy, it still breaks:

    #!/usr/bin/perl use strict; my @buf = <DATA>; for my $i ( 0 .. $#buf ) { if ($buf[$i] =~ s/^\s*<jobnumber>(.*)<\/jobnumber>\s*$/$1/s) { $buf[$i+1] =~ s/^\s*<location>\s*(.*)\s<location>\s*$/$1/s; # if your tags are really like this &process($buf[$i],$buf[$i+1]); } } sub process { my ($jobnumber,$location) = @_; print "Found a job $jobnumber in $location.\n"; # do something } __DATA__ <posts> <post> <jobnumber> 1234 </jobnumber> <location>Somecity, NJ</location> </post> <post> <jobnumber>87922</jobnumber> <location>Othercity, AK</location> </post> </posts>

    See Death to Dot Star! for the explanation of why your regex fails (and for some excellent examples of how I have screwed up regexes on delimited text).

    Use a parser for data like this. Regexes, while I love them, are for matching data, not parsing it.

    As for your 'related note', it doesn't work because you have (.?) in your code. The dot/question mark makes you match one character and have that match optional. It's equivalent to (.{0,1}).


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Thanks Ovid, you're right and I'll reread that article!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-01-29 06:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found