Perl XML::Simple Accessing complex XML

Z

zzapper

Hi
<companyname count="1">
<property id="55467" md="2008-03-20" mc="GBP" mp="173000" >
<psumm><![CDATA[This 3 bedroom, ]]></psumm>
<a1>1 Bucket Way</a1>
<at>Stafford</at>
<ac>Staffordshire</ac>
<actry></actry>
<images>
<image id="543">Property Image</image>
<image id="545">Property Image</image>
</images>
</property>
</companyname>

I am trying to read the above (simplified) with XML::Simple with or
without ForceArray=1.

I can output everything EXCEPT I cannot find the Perl data syntax to
access anything in <images> eg 543 or
the text "Property Image"

$data = $xml->XMLin("prop.xml", forcearray => 1);
$ac=$data->{property}->{554674}->{ac}[0];

Desperate!!
zzapper
 
J

John

zzapper said:
Hi
<companyname count="1">
<property id="55467" md="2008-03-20" mc="GBP" mp="173000" >
<psumm><![CDATA[This 3 bedroom, ]]></psumm>
<a1>1 Bucket Way</a1>
<at>Stafford</at>
<ac>Staffordshire</ac>
<actry></actry>
<images>
<image id="543">Property Image</image>
<image id="545">Property Image</image>
</images>
</property>
</companyname>

I am trying to read the above (simplified) with XML::Simple with or
without ForceArray=1.

I can output everything EXCEPT I cannot find the Perl data syntax to
access anything in <images> eg 543 or
the text "Property Image"

$data = $xml->XMLin("prop.xml", forcearray => 1);
$ac=$data->{property}->{554674}->{ac}[0];

Desperate!!
zzapper

Hi

With ForceArray turned on.

my $temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{id}; # id of
first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{id}; # id of
second image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{content}; #
Property Image of first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{content}; #
Property Image of second image

Regards
John
 
Z

zzapper

Hi
<companyname count="1">
<property id="55467" md="2008-03-20" mc="GBP" mp="173000" >
<psumm><![CDATA[This 3 bedroom, ]]></psumm>
<a1>1 Bucket Way</a1>
<at>Stafford</at>
<ac>Staffordshire</ac>
<actry></actry>
<images>
    <image id="543">Property Image</image>
    <image id="545">Property Image</image>
</images>
</property>
</companyname>
I am trying to read the above (simplified) with XML::Simple with or
without ForceArray=1.
I can output everything EXCEPT I cannot find the Perl data syntax to
access anything in <images> eg  543 or
the text "Property Image"
$data = $xml->XMLin("prop.xml", forcearray => 1);
$ac=$data->{property}->{554674}->{ac}[0];
Desperate!!
zzapper

Hi

With ForceArray turned on.

my $temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{id}; # id of
first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{id}; # id of
second image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{content}; #
Property Image of first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{content}; #
Property Image of second image

Regards
John

Hi John
Unfortunately this didn't work for me, the output was blank.
zzapper
 
J

Jim Gibson

zzapper said:
Hi
<companyname count="1">
<property id="55467" md="2008-03-20" mc="GBP" mp="173000" >
<psumm><![CDATA[This 3 bedroom, ]]></psumm>
<a1>1 Bucket Way</a1>
<at>Stafford</at>
<ac>Staffordshire</ac>
<actry></actry>
<images>
    <image id="543">Property Image</image>
    <image id="545">Property Image</image>
</images>
</property>
</companyname>
I am trying to read the above (simplified) with XML::Simple with or
without ForceArray=1.
I can output everything EXCEPT I cannot find the Perl data syntax to
access anything in <images> eg  543 or
the text "Property Image"
$data = $xml->XMLin("prop.xml", forcearray => 1);
$ac=$data->{property}->{554674}->{ac}[0];
Desperate!!
zzapper

Hi

With ForceArray turned on.

my $temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{id}; # id of
first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{id}; # id of
second image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{content}; #
Property Image of first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{content}; #
Property Image of second image

Regards
John

Hi John
Unfortunately this didn't work for me, the output was blank.
zzapper

Try:

$data->{property}->{'55467'}->{images}->[0]->{image}->{'545'}->{content}

To figure these things out, use the Data::Dumper module to reveal the
structure of the data returned by XMLin:

print Dumper(\$data);
 
Z

zzapper

zzapper said:
Hi
<companyname count="1">
<property id="55467" md="2008-03-20" mc="GBP" mp="173000" >
<psumm><![CDATA[This 3 bedroom, ]]></psumm>
<a1>1 Bucket Way</a1>
<at>Stafford</at>
<ac>Staffordshire</ac>
<actry></actry>
<images>
    <image id="543">Property Image</image>
    <image id="545">Property Image</image>
</images>
</property>
</companyname>
I am trying to read the above (simplified) with XML::Simple with or
without ForceArray=1.
I can output everything EXCEPT I cannot find the Perl data syntax to
access anything in <images> eg  543 or
the text "Property Image"
$data = $xml->XMLin("prop.xml", forcearray => 1);
$ac=$data->{property}->{554674}->{ac}[0];
Desperate!!
zzapper
Hi
With ForceArray turned on.
my $temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{id}; #id of
first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{id}; # idof
second image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[0]{content};#
Property Image of first image
$temp=$data->{property}->[0]->{images}->[0]->{image}->[1]{content};#
Property Image of second image
Regards
John
Hi John
Unfortunately this didn't work for me, the output was blank.
zzapper

Try:

$data->{property}->{'55467'}->{images}->[0]->{image}->{'545'}->{content}

To figure these things out, use the Data::Dumper module to reveal the
structure of the data returned by XMLin:

print Dumper(\$data);
Great that worked.
I've now tried an alternative
$data = $xml->XMLin("prop.xml", KeyAttr => {images => 'image'});
Which works if don't use forcearray.

gives me

'images' => {
'image' => [
{
'content' =>
'Property Image',
'id' => '2410543'
},
{
'content' =>
'Property Image',
'id' => '2410544'
},
{
'content' =>
'Property Image',
'id' => '2410545'
}
]


zzapper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top