Hi,
I'm trying to loop through an XML file, like so:
<?xml version="1.0" encoding="utf-8"?><zanox>
<product id="2726635862" number="132727368">stuff here</product>
<product id="2726635863" number="132727368">stuff here</product>
<product id="2726635864" number="132727368">stuff here</product>
</zanox>
</zanox>
The problem is, going through each value :/
I'm using this Perl:
...and that gives a dump of (cleaned up a little, to make it easier to read =))
$VAR1 = {
'product' => {
'2726635862' => {
},
'2726635863' => {
},
'2726635864' => {
}
}
};
Now, I'm trying to work out how I "loop" through this data?
I've tried:
$ref->{product}[0]
..but that gives an undef value, when dumping by Data:
umper.
I've tried doing a "map" with:
...but that give an error:
I'm at a bit of a loss as to how I should approach this?
TIA!
Andy
I'm trying to loop through an XML file, like so:
<?xml version="1.0" encoding="utf-8"?><zanox>
<product id="2726635862" number="132727368">stuff here</product>
<product id="2726635863" number="132727368">stuff here</product>
<product id="2726635864" number="132727368">stuff here</product>
</zanox>
</zanox>
The problem is, going through each value :/
I'm using this Perl:
Code:
use Data::Dumper;
my $path = './ProductData.xml';
my $page;
open(READIT,"<$path") || die qq|Cant read $path: $!|;
while (<READIT>) {
$page .= $_;
}
close(READIT);
use XML::Simple;
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
my $ref = XMLin($page);
map { print $_ . "\n" } keys $ref->{product};
...and that gives a dump of (cleaned up a little, to make it easier to read =))
$VAR1 = {
'product' => {
'2726635862' => {
},
'2726635863' => {
},
'2726635864' => {
}
}
};
Now, I'm trying to work out how I "loop" through this data?
I've tried:
$ref->{product}[0]
..but that gives an undef value, when dumping by Data:
I've tried doing a "map" with:
Code:
map { print $_ . "\n" } keys $ref->{product};
...but that give an error:
ultranerds@east script_dev $ perl test.cgi
Content-type: text/html
<h1>Software error:</h1>
<pre>Type of arg 1 to keys must be hash (not hash element) at test.cgi line 25, near "};"
Execution of test.cgi aborted due to compilation errors.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.
</p>
[Sat Apr 26 05:20:25 2008] test.cgi: Type of arg 1 to keys must be hash (not hash element) at test.cgi line 25, near "};"
[Sat Apr 26 05:20:25 2008] test.cgi: Execution of test.cgi aborted due to compilation errors.
I'm at a bit of a loss as to how I should approach this?
TIA!
Andy