Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLin - interesting format :/

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
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:

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::Dumper.

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 &quot;};&quot;
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
 
Hi,

I managed to get around it by removing the

id="12345" number="12345"

..bit, with:

s|id="\d+" number="\d+"||sig;

Seems to be ok now - although its a hell of a confusing XML feed :D

Code:
     $add->{Title}    = $ref->{product}[$i]->{info}->{name};
     $add->{Category} = $ref->{product}[$i]->{category};
     $add->{URL}      = $ref->{product}[$i]->{links}->{deeplink};
     $add->{Description} = $ref->{product}[$i]->{info}->{description}[1];
     $add->{Image}       = $ref->{product}[$i]->{links}->{image}[0]->{url};
     $add->{Image2}      = $ref->{product}[$i]->{links}->{image}[1]->{url};
     $add->{Price}       = $ref->{product}[$i]->{offer}->{currentprice}->{content};
     $add->{Currency}    = $ref->{product}[$i]->{offer}->{currentprice}->{currency};

Ah well - ce la vi =)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top