***** infoCopter.com/perl *****
XML::GDOMEXML::GDOME - Interface to Level 2 DOM gdome2 library $Revision: 1.2 $[ up ] [ XML::GDOME at CPAN ] - [ External XML Resources ] - [ Search for XML here ]
Installation of XML::GDOME can be somewhat tricky since it has a couple of dependencies to
other libraries and tools that in special cases have to be forced updated due to further dependencies (especially the glib-2.x stuff that requires pkg-config tool) and so on. Quick Installation Guide for gdome2
Sample #1Personally, I suggest to consider this example first because it's the most generic approach I could determine. The GDOME Perl bundle is not widely documented yet but for the most cooking it will cover your XML needs out of the box. The syntax is straight-forward. Besides constructing an object with XML::GDOME->createDocFromURI() there is also XML::GDOME->createDocFromString() available which you might prefer in implementations that fetch XML code first through LWP and then do the XML parsing stuff. #!/usr/bin/perl -w
use strict;
use XML::GDOME;
my $doc = XML::GDOME->createDocFromURI($ARGV[0]);
my @nodes = $doc->findnodes("//*");
foreach my $node (@nodes) {
my @childs = $node->childNodes;
foreach my $child (@childs) {
if($child->nodeType == ELEMENT_NODE) {
my $data = defined $child->firstChild() ?
$child->firstChild()->data : 'NULL';
print "node = '", $child->nodeName, "', \tdata='$data'\n";
}
}
}
Example with XML processing from a file on the same machine#!/usr/bin/perl -w
use strict;
use XML::GDOME;
# my $doc = XML::GDOME->createDocFromURI($ARGV[0]);
my $doc = XML::GDOME->createDocFromString(${&slurpFile($ARGV[0])});
my @nodes = $doc->findnodes("//*");
foreach my $node (@nodes) {
my @childs = $node->childNodes;
foreach my $child (@childs) {
if($child->nodeType == ELEMENT_NODE) {
my $data = defined $child->firstChild() ?
$child->firstChild()->data : 'NULL';
print "node = '", $child->nodeName, "', \tdata='$data'\n";
}
}
}
sub slurpFile($) {
local $/ = undef;
open(IN, "$_[0]") or die $!;
my $file = <IN>;
close IN;
\$file;
}
Another Sample
#!/usr/bin/perl -w
use strict;
use XML::GDOME;
##### PROTO
sub getsubtag($);
##### GLOBAL
my $doc = XML::GDOME->createDocFromURI($ARGV[0]);
###################################################
# MAIN
###################################################
my $subTag = getsubtag ($ARGV[1]);
my $argName = $ARGV[2] || '';
print "-> subtag = '$subTag'\n\n";
my @nodelist = $doc->getElementsByTagName($subTag);
foreach my $node(@nodelist) {
my $arg = $node->getAttribute($argName) || '';
print "$subTag / $argName = '$arg'\n" if $arg;
my @childs = $node->childNodes;
foreach my $child(@childs) {
if($child->nodeType == ELEMENT_NODE) {
print "-> node = '", $child->nodeName, "', data='",
$child->firstChild()->data, "'\n";
}
}
}
###################################################
sub getsubtag ($) {
###################################################
my $supertag = $_[0];
my $subtag = '';
my @nodelist = $doc->getElementsByTagName($supertag);
OUTER: foreach my $node(@nodelist) {
my @childs = $node->childNodes;
foreach my $child(@childs) {
if($child->nodeType == ELEMENT_NODE) {
$subtag = $child->nodeName;
last OUTER;
}
}
}
$subtag;
}
__END__
USAGE:
./xmlparse.pl data.xml addrbook
|