RE: help me in reading the xml file
- From: thomas-a.reddy@xxxxxx (Allam Reddy)
- Date: Mon, 21 Jan 2008 16:52:11 +0530
Thanks Chas Owens for your detailed explanation.
It helped me a lot
Thanks,
Thomas Reddy
-----Original Message-----
From: Chas. Owens [mailto:chas.owens@xxxxxxxxx]
Sent: Friday, January 18, 2008 9:01 PM
To: Allam Reddy, Thomas
Cc: beginners@xxxxxxxx
Subject: Re: help me in reading the xml file
On Jan 18, 2008 4:54 AM, Allam Reddy, Thomas <thomas-a.reddy@xxxxxx>
wrote:
Hi Chas Owens,snip
Thanks for your reply.
Is there a way to parse and get the info from xml withou using
XML::Twig?
There are many XML parsing modules in Perl; unfortunately, none of the
are part of Core Perl. In fact, half of Perl's power as a language
comes from modules you install from CPAN. If you intend to use Perl you
really need to learn how to install modules. The simplest XML parser I
can find is XML::Tiny. It has no dependencies and is written in pure
Perl. You can download it from CPAN* and install it (without root
privileges**) like this:
gzip -dc /path/to/download/XML-Tiny-1.11.tar.gz | tar xf cd
XML-Tiny-1.11/ perl Makefile.PL PREFIX=~/perl make make test make
install
Note, if make test reports any errors you should not install the module.
Now that the module is installed in your home directory, you need to
tell Perl where to find it
export PERL5LIB=~/perl/lib/perl5/site_perl:$PERL5LIB
You can now run Perl scripts that need XML::Tiny. This one is similar
in results to the one I sent you that used XML::Twig:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Tiny qw<parsefile>;
#for every filename passed to us on the command line #open the file,
parse it into a tree, and then walk #the tree printing the contents of
the #descriptor-file-name tag for my $file (@ARGV) {
open my $fh, "<", $file
or die "could not open $file: $!";
my $tree = parsefile($fh);
walk_tree($tree->[0], \&print_descriptor_file_name_content);
}
#takes an element
#if the elements name is descriptor-file-name #then print out all its
childrens contents #(there should be only one, but you never
#know)
sub print_descriptor_file_name_content {
my ($elt) = @_;
#if $elt->{content} is not a reference
#then it can't be descended into and if
#it is an empty descriptor-file-name
#tag, then it doesn't have anything to
#print anyway
return 0 unless ref $elt->{content};
if ($elt->{name} eq "descriptor-file-name") {
print "$_->{content}\n" for @{$elt->{content}};
}
return 1;
}
#takes a tree and a qualifying function
#descends depth-first down a tree
#elements that return false from
#the qualifying function are not
#descended into
sub walk_tree {
my ($tree, $sub) = @_;
for my $elt (@{$tree->{content}}) {
walk_tree($elt, $sub) if $sub->($elt);
}
}
*
http://search.cpan.org/CPAN/authors/id/D/DC/DCANTRELL/XML-Tiny-1.11.tar.
gz
** if you have root privledges then leave off the PREFIX and Perl will
find the right directory to install it in. In that case you also do not
need the PERL5LIB environmental variable.
.
- References:
- help me in reading the xml file
- From: Allam Reddy
- Re: help me in reading the xml file
- From: Chas. Owens
- RE: help me in reading the xml file
- From: Allam Reddy
- Re: help me in reading the xml file
- From: Chas. Owens
- RE: help me in reading the xml file
- From: Allam Reddy
- Re: help me in reading the xml file
- From: Chas. Owens
- help me in reading the xml file
- Prev by Date: Perl script to lookup IP addresses
- Next by Date: Re: Perl script to lookup IP addresses
- Previous by thread: Re: help me in reading the xml file
- Next by thread: Help in system function
- Index(es):
Relevant Pages
|
|