freelanceprogrammers.org Forum Index » Perl

parsing XML


View user's profile Post To page top
syedqasimrazaa Posted: Fri May 21, 2004 2:16 pm


Joined: 02 May 2004

Posts: 3
parsing XML
HI!

I am new to perl language and want to parse an xml file. Can some
body send me a sample code that how perl parses an xml file .


I have tried to search over the net but the examples there are very
complex.

Thanks

QR
Reply with quote
Send private message
View user's profile Post To page top
merlynstoneh... Posted: Fri May 21, 2004 8:46 pm


Joined: 21 May 2004

Posts: 44
parsing XML
>>>>> "Syed" == Syed Qasim Raza <qasimraza@...> writes:

Syed> I am new to perl language and want to parse an xml file. Can some
Syed> body send me a sample code that how perl parses an xml file .


Syed> I have tried to search over the net but the examples there are very
Syed> complex.

That`s because parsing XML *is* complex. :)

What do you want to do with the XML? Where is it from? Are you going
to modify it and send it back out?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Reply with quote
Send private message
View user's profile Post To page top
perl_jam03 Posted: Sat May 22, 2004 12:41 am


Joined: 05 May 2004

Posts: 16
parsing XML
Try XML::Simple. I have never used it on Windows, but on linux it works
just fine. Install it using CPAN. It works by parsing a XML source
file, and building a XML::Simple object that contains a hashref with
the contents of the XML tree.

Read the documentation. It should be straigtforward. From it I got the
following:

<doc-snapshot>

use XML::Simple;

my $xs = new XML::Simple(options);
my $ref = $xs->XMLin([<xml file or string>] [, <options>]);

------------

QUICK START
Say you have a script called foo and a file of configuration
options called foo.xml containing this:

<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
<server name="sahara" osname="solaris" osversion="2.6">
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server name="gobi" osname="irix" osversion="6.5">
<address>10.0.0.102</address>
</server>
<server name="kalahari" osname="linux" osversion="2.0.34">
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</config>

The following lines of code in foo:

use XML::Simple;

my $config = XMLin();

will `slurp` the configuration options into the hashref $config
(because no arguments are passed to
"XMLin()" the name and location of the XML file will be inferred
from name and location of the
script). You can dump out the contents of the hashref using
Data::Dumper:

use Data::Dumper;

print Dumper($config);

which will produce something like this (formatting has been
adjusted for brevity):

{
`logdir` => `/var/log/foo/`,
`debugfile` => `/tmp/foo.debug`,
`server` => {
`sahara` => {
`osversion` => `2.6`,
`osname` => `solaris`,
`address` => [ `10.0.0.101`, `10.0.1.101` ]
},
`gobi` => {
`osversion` => `6.5`,
`osname` => `irix`,
`address` => `10.0.0.102`
},
`kalahari` => {
`osversion` => `2.0.34`,
`osname` => `linux`,
`address` => [ `10.0.0.103`, `10.0.1.103` ]
}
}
}

Your script could then access the name of the log directory like
this:

print $config->{logdir};

similarly, the second address on the server `kalahari` could be
referenced as:

print $config->{server}->{kalahari}->{address}->[1];


</doc-snapshot>

Good luck
Rex


--- Syed Qasim Raza <qasimraza@...> wrote:
> HI!
>
> I am new to perl language and want to parse an xml file. Can some
> body send me a sample code that how perl parses an xml file .
>
>
> I have tried to search over the net but the examples there are very
> complex.
>
> Thanks
>
> QR
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help