Home : General : Perl Programming :

General: Perl Programming: Re: [Andy] XML::Simple...: Edit Log

Here is the list of edits for this post
Re: [Andy] XML::Simple...
Hey Andy,

This works for me:
Code:
#!/usr/bin/perl
use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

my $xml = q~<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>
<product>
<name><![CDATA[OLYMPUS MJU 410 ZOOM DIGITAL CAMERA]]></name>
<description><![CDATA[ OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ]]></description>
<imageUrl><![CDATA[ http://www.site.com/images/products/t_OLYMJUD410Z.gif]]></imageUrl>
<productUrl><![CDATA[http://www.site.com/click?a=322582&p=17211&prod=5908504]]></productUrl>
<price>279.9</price>
<currency>GBP</currency>
<TDProductId>5908504</TDProductId>
<TDCategories>
<TDCategory>
<name><![CDATA[Digital Compact Cameras]]></name>
</TDCategory>
</TDCategories>
<fields></fields>
</product>
</products>
~;

print "content-type: text/html\n\n";
print '<pre>', $/;
print 'XML::Simple version: ', $XML::Simple::VERSION, $/;
print Dumper(XMLin($xml, KeepRoot => 1, KeyAttr => 'Product'));
print '</pre>', $/;

and produces:
Code:
XML::Sinmple version: 2.09
$VAR1 = {
'products' => {
'product' => [
{
'name' => 'OLYMPUS MJU 410 ZOOM DIGITAL CAMERA',
'TDCategories' => {
'TDCategory' => {
'name' => 'Digital Compact Cameras'
}
},
'description' => ' OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ',
'currency' => 'GBP',
'imageUrl' => ' http://www.site.com/images/products/t_OLYMJUD410Z.gif',
'fields' => {},
'TDProductId' => '5908504',
'productUrl' => 'http://www.site.com/click?a=322582&p=17211&prod=5908504',
'price' => '279.9'
},
{
'name' => 'OLYMPUS MJU 410 ZOOM DIGITAL CAMERA',
'TDCategories' => {
'TDCategory' => {
'name' => 'Digital Compact Cameras'
}
},
'description' => ' OLYMPUS MJU 410 ZOOM DIGITAL CAMERA ',
'currency' => 'GBP',
'imageUrl' => ' http://www.site.com/images/products/t_OLYMJUD410Z.gif',
'fields' => {},
'TDProductId' => '5908504',
'productUrl' => 'http://www.site.com/click?a=322582&p=17211&prod=5908504',
'price' => '279.9'
}
]
}
};

~Charlie

[edit]To answer your question, Products was the root which isn't part of the hash by default. If you add the KeepRoot => 1 option it keeps Products in there. I also added KeyAttr => 'Product' so that both products display; they are identical so they would overwrite each other inthe hash otherwise.[/edit]

Last edited by:

Chaz: Jul 12, 2004, 9:54 AM

Edit Log: