Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > XML::Simple Parsing with Attributes problem

Reply
Thread Tools

XML::Simple Parsing with Attributes problem

 
 
John
Guest
Posts: n/a
 
      02-03-2006
Hi - problem parsing with an attribute name.

<england>
<town code="LON">Capital City</town>
<town code="MAN">Manchester</town>
<town code="BHAM">Birmingham</town>
</england>

I'm using XML::Simple.

$xml = new XML::Simple (ForceArray=>1, suppressempty=>1);
$data = $xml->XMLin($xmlfile);

$which=$data->{england}->[0]->{town}->[0]; doesn't work.

I need to access both attribute value and its contents (BHAM and
Birmingham).

Any ideas?

John


 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      02-03-2006
Many thanks. I had forgoten about the Dumper.

I had an *array * of hashes.

Corrected line should be (in case anybody else reads the thread) as
follows:-

$key=$data->{england}->[0]->{town}->[$i]{code};
$value=$data->{england}->[0]->{town}->[$i]{contents};

for $i = 1 to num.

Regards
John



"Bernard El-Hagin" <(E-Mail Removed)> wrote in
message news:Xns975F8471CAEFDelhber1lidotechnet@10.232.40. 227...
> "John" <(E-Mail Removed)> wrote:
>
>> Hi - problem parsing with an attribute name.
>>
>> <england>
>> <town code="LON">Capital City</town>
>> <town code="MAN">Manchester</town>
>> <town code="BHAM">Birmingham</town>
>> </england>
>>
>> I'm using XML::Simple.
>>
>> $xml = new XML::Simple (ForceArray=>1, suppressempty=>1);
>> $data = $xml->XMLin($xmlfile);
>>
>> $which=$data->{england}->[0]->{town}->[0]; doesn't work.
>>
>> I need to access both attribute value and its contents (BHAM and
>> Birmingham).
>>
>> Any ideas?

>
>
> Print out the structure XML::Simple creates (using Data:umper) and see
> for yourself. I suspect it
> should be something along the lines of
>
> $data->{'england'}->{'town'}->[0]->{'code'}
>
> to get the code of the first town.
>
>
> --
> Cheers,
> Bernard



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
XML::Simple Parsing with Attributes problem John Perl Misc 1 02-03-2006 04:39 PM



Advertisments