Perl XML::Simple Accessing complex XML

Discussion in 'Perl Misc' started by zzapper, Jun 18, 2008.

  1. zzapper

    zzapper Guest

    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
     
    zzapper, Jun 18, 2008
    #1
    1. Advertising

  2. zzapper

    John Guest

    "zzapper" <> wrote in message
    news:...
    > 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
     
    John, Jun 18, 2008
    #2
    1. Advertising

  3. zzapper

    zzapper Guest

    On Jun 18, 1:39 pm, "John" <> wrote:
    > "zzapper" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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
     
    zzapper, Jun 18, 2008
    #3
  4. zzapper

    Jim Gibson Guest

    In article
    <>,
    zzapper <> wrote:

    > On Jun 18, 1:39 pm, "John" <> wrote:
    > > "zzapper" <> wrote in message
    > >
    > > news:...
    > >
    > >
    > >
    > > > 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);

    --
    Jim Gibson
     
    Jim Gibson, Jun 18, 2008
    #4
  5. zzapper

    zzapper Guest

    On Jun 18, 8:33 pm, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    >
    >
    > zzapper <> wrote:
    > > On Jun 18, 1:39 pm, "John" <> wrote:
    > > > "zzapper" <> wrote in message

    >
    > > >news:....

    >
    > > > > 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);
    >
    > --
    > Jim Gibson

    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
     
    zzapper, Jun 18, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,815
  2. news.amnet.net.au
    Replies:
    1
    Views:
    581
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  3. Stanimir Stamenkov
    Replies:
    2
    Views:
    749
    Stanimir Stamenkov
    Oct 25, 2005
  4. Robert Mark Bram
    Replies:
    0
    Views:
    686
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    770
Loading...

Share This Page