in reply to Pasring XML into a simple hash
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?#!/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 }
$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 | |
by mattr (Curate) on Jun 22, 2001 at 14:05 UTC |