***** infoCopter.com/perl *****

XML::GDOME

XML::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.

Even on a late Linux distribution like RedHat9 a simple perl -MCPAN -e "install XML::GDOME" mostly doesn't lead to success if you try this from the beginning. So continue to read a very quick installation guide below.

If you're going to setup this Module on a RedHat system release 7.3 and below, I suggest to download and install gdome2-0.7.2, and not a newer release!



Quick Installation Guide for gdome2

  1. Check if glib2 is already installed on your system:
    pkg-config --modversion glib-2.0
    A good response would be something like 2.x.y

    On a RedHat9, it's probably enough just to compile glib-2.2.1. Notice: re-logon after compiling glib-2.2.1 so the environment variables are being reloaded before you recheck with this command!

    If the required version is not shown, you should download and install freetype-2.x.y-z (./configure), pango-1.0.x, gtk+-2.0.x and glib-2.0.x (./configure --prefix=/usr).
    Use ./configure --prefix=/usr/lib on RedHat8 systems

  2. Dependencies: libxml, libxslt
    (Note: You could skip to the next point on a common RedHat8 installation)
    See Compatibility Table below if you're unsure.
    rpm -Uhv libxml-1.8.14-2.i386.rpm
    rpm -Uhv libxslt-1.0.19-1.i386.rpm

  3. gdome
    http://gdome2.cs.unibo.it/#downloads
    cd gdome2-0.7.2
    ./configure
    make
    make check
    make install

  4. If necessary, check if a development version of glib and gdome is available for the next step. You might try to install rpm's if other actions should fail:
    rpm -Uhv glib-devel-1.2.10-8.i386.rpm
    rpm -Uhv gdome2-devel-0.7.2-1.i386.rpm

  5. Last check before installing XML::GDOME:
    gdome-config --libs
    << -L/usr/lib -lgdome -lglib -lxml2 -lz -lm

    If you got error messages here, the Makefile procedure won't work correctly, so you had to patch gdome-config

  6. If not installed on your system yet, install these PREREQuisites before continue:
    XML::SAX
    XML::LibXML::Common

  7. Finally
    perl -MCPAN -e "install XML::GDOME"

    Note: They involved further tests from XML::GDOME Version 0.86 meanwhile which probably not all of them will pass. You could do a force install or download XML::GDOME Version 0.85 which will work excellent according to the Compatibility Table below.



Sample #1

Personally, 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

XML::GDOME Compatibility Table $Revision: 1.2 $
RedHat9
gtk 2.2
1949430 Apr 28 11:08 glib-2.2.1.tar.gz
 610697 Apr 28 11:02 pkgconfig-0.15.0.tar.gz
pkg-config --modversion glib-2.0
2.2.1
xml gdome2
113623 Apr 28 10:51 gdome2-0.7.2-1.i386.rpm
469868 Apr 28 10:51 gdome2-0.7.2-1.src.rpm
225338 Apr 28 10:51 gdome2-devel-0.7.2-1.i386.rpm
114681 Apr 28 10:52 glib-devel-1.2.10-8.i386.rpm


Probably these 4 rpm's are already installed on a standard RH9 system so just check with:

rpm -qa|grep xml
libxml2-2.5.4-3.rh9
libxml2-devel-2.5.4-3.rh9
libxml-1.8.17-8
libxml-devel-1.8.17-8

XML-GDOME-0.85
© reto :)