For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > parse xml









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author parse xml
E.Horn

2004-10-25, 3:56 pm

Hey!
How can i pars this xmlfile?
i just want to have the <MenueName>geo</MenueName> and the count out of
this...

<ResultItem>
<DbName>geo</DbName>
<MenuName>GEO Profiles</MenuName>
<Count>69408</Count>
<Status>Ok</Status>
</ResultItem>

<ResultItem>
<DbName>gds</DbName>
<MenuName>GEO DataSets</MenuName>
<Count>8</Count>
<Status>Ok</Status>
</ResultItem>

regards eve

Philipp Traeder

2004-10-25, 3:56 pm

On Monday 25 October 2004 16:27, E.Horn wrote:
> Hey!
> How can i pars this xmlfile?
> i just want to have the <MenueName>geo</MenueName> and the count out of
> this...
>

[..]

Hi,

I can highly recommend XML::Simple - just use it like this:

#!/usr/bin/perl -w

use strict;
use XML::Simple;
use Data::Dumper;

my $ref = XMLin('xmlin.xml');

# now you've got the whole XML file in $ref:
print Dumper($ref);
print "\n";

print $ref->{'ResultItem'}->[0]{'MenuName'};
print "\t";
print $ref->{'ResultItem'}->[0]{'Count'} . "\n";

__END__

Now you can walk over the hash/array structure and everything's fine :-)

HTH,

Philipp

Jenda Krynicky

2004-10-25, 3:56 pm

From: "Traeder, Philipp" <Traeder@immobilienscout24.de>
> print $ref->{'ResultItem'}->[0]{'MenuName'};


I think it's better to either skip all the -> between subscriptions
or include them all:

print $ref->{'ResultItem'}[0]{'MenuName'};
or
print $ref->{'ResultItem'}->[0]->{'MenuName'};

It doesn't really matter of course.

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Philipp Traeder

2004-10-25, 3:56 pm

On Monday 25 October 2004 16:49, Jenda Krynicky wrote:
> From: "Traeder, Philipp" <Traeder@immobilienscout24.de>
>
>
> I think it's better to either skip all the -> between subscriptions
> or include them all:
>
> print $ref->{'ResultItem'}[0]{'MenuName'};
> or
> print $ref->{'ResultItem'}->[0]->{'MenuName'};
>


You're right - I should have taken another look at this before posting.
My main focus was on pointing out XML::Simple combined with Data::Dumper -
though this is *not* an excuse ;-).

Philipp
Franklin

2004-10-28, 3:55 am

I tried this one , but it seems not working:(
Not an ARRAY reference at test.pl line 8
----- Original Message -----
From: "Philipp Traeder" <philipp@hitchhackers.net>
To: <beginners@perl.org>
Sent: Monday, October 25, 2004 4:36 PM
Subject: Re: parse xml


> On Monday 25 October 2004 16:49, Jenda Krynicky wrote:
>
> You're right - I should have taken another look at this before posting.
> My main focus was on pointing out XML::Simple combined with Data::Dumper -
> though this is *not* an excuse ;-).
>
> Philipp
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>


Philipp Traeder

2004-10-28, 8:55 am

Hi Franklin,

please bottom post (write your answer below the quoted text) - it's much
easier to follow a thread this way.

On Thursday 28 October 2004 02:11, Franklin wrote:
[color=darkred]
> I tried this one , but it seems not working:(
> Not an ARRAY reference at test.pl line 8


Taking a look at the other thread you opened on this list, my first guess is
that your input files differ (your new file seems to have "ResultItemOne" as
top level element while your original post's top level element was called
"ResultItem").

I'd suggest to read the XML file via XML::Simple and to dump it via
Data::Dumper - then you can see what the data structure looks like. After
that you just need to play around a bit with array and hash references. If
you run into problems with this, post the output of Data::Dumper to the list
and somebody will be able to help.

HTH,

Philipp

Franklin

2004-10-28, 8:55 am

I have gotten it done. Thanks
----- Original Message -----
From: "Philipp Traeder" <philipp@hitchhackers.net>
To: <beginners@perl.org>
Sent: Thursday, October 28, 2004 5:40 AM
Subject: Re: parse xml


> Hi Franklin,
>
> please bottom post (write your answer below the quoted text) - it's much
> easier to follow a thread this way.
>
> On Thursday 28 October 2004 02:11, Franklin wrote:
>
>
> Taking a look at the other thread you opened on this list, my first guess
> is
> that your input files differ (your new file seems to have "ResultItemOne"
> as
> top level element while your original post's top level element was called
> "ResultItem").
>
> I'd suggest to read the XML file via XML::Simple and to dump it via
> Data::Dumper - then you can see what the data structure looks like. After
> that you just need to play around a bit with array and hash references. If
> you run into problems with this, post the output of Data::Dumper to the
> list
> and somebody will be able to help.
>
> HTH,
>
> Philipp
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com