XML technologies / XML / Perl & XML / Tree-oriented processing / XML::SimpleObject
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
Using built-in data types has advantages, but quickly leads to complex, hard-to-read XML code. When finding an element’s last child or changing the data representation, an object interface soon becomes appealing. Several object-oriented XML modules are therefore worth examining.
We begin our tour of object-oriented XML trees with Dan Brian’s module XML::SimpleObject. It takes the data structure generated by XML::Parser in tree mode and converts it into an object hierarchy. Each object represents an element and provides methods to access child nodes. As with XML::Simple we access child elements by name, this time passing those names to suitable methods.
How is the module useful? The example below is a simple XML file containing a family tree. We will write a program to convert it into an object tree, then traverse that tree to output a textual description.
Example: A family tree
<?xml version="1.0" encoding="ISO-8859-1"?>
<stammbaum>
<nachkomme><name>Gluk der Große</name>
<kinder>
<nachkomme><name>Glim der Tapfere</name></nachkomme>
<nachkomme><name>Gelbar der Starke</name></nachkomme>
<nachkomme><name>Glurko der Gute</name>
<kinder>
<nachkomme><name>Glurf der Ungeduldige</name></nachkomme>
<nachkomme><name>Glug der Merkwürdige</name>
<kinder>
<nachkomme><name>Blug der Kranke</name></nachkomme>
<nachkomme><name>Flug der Gestörte</name></nachkomme>
</kinder>
</nachkomme>
</kinder>
</nachkomme>
</kinder>
</nachkomme>
</stammbaum>The next example contains our program. First, the file is read with XML::Parser in tree mode. The result is passed to the XML::SimpleObject constructor. Next comes a function schaueauf( ), which recursively traverses the tree and outputs text. For each <nachkomme> element, the name is printed; if it has descendants, its subtree is traversed too. To check this, we test whether the method child returns the value undef or not.
Example: A program using XML::SimpleObject
use XML::Parser;
use XML::SimpleObject;
# Lesen der Eingabedatei und Aufbau des Baums
my $file = shift @ARGV;
my $parser = XML::Parser->new( ErrorContext => 2, Style => "Tree" );
my $tree = XML::SimpleObject->new( $parser->parsefile( $file ));
# Ausgabe des Stammbaums
print "Mein Stammbaum beginnt mit ";
schaueauf( $tree->child( 'stammbaum' )->child( 'nachkomme' ), '' );
# Ausgabe einer Generation des Stammbaums
sub schaueauf {
my( $anc, $indent ) = @_;
# Ausgabe des Namens des Vaters
print $indent . $anc->child( 'name' )->value;
# Falls Kinder vorhanden sind, gib sie ebenfalls aus
if( $anc->child( 'kinder' ) and $anc->child( 'kinder' )->children ) {
print ", Vater von...\n";
my @children = $anc->child( 'kinder' )->children;
foreach my $child ( @children ) {
schaueauf( $child, $indent . ' ' );
}
} else {
print "\n";
}
}Here is the output demonstrating that the program works. It indents lines slightly for readability:
Mein Stammbaum beginnt mit Gluk dem Großen, Vater von...
Glim der Tapfere
Gelbar der Starke
Glurko der Gute, Vater von...
Glurf der Ungeduldige
Glug der Merkwürdige, Vater von...
Blug der Kranke
Flug der Gestörte We used several methods to access the data in each object. child( ) returns a reference to an instance of XML::SimpleObject, representing a child element of the calling node. children( ) returns a list of all such references. value( ) returns the calling node’s text data as a scalar value. Passing arguments to these methods restricts the search. child( 'name' ) for example, returns a representation of the element <name> if present; otherwise it returns undef.
This is a good start, but as its name suggests, the module is too simple for many applications. Access to individual nodes is limited: essentially, you can get one child element or a list of children. Multiple elements with the same name already cause problems.
The module also cannot convert the parsed tree back to XML. Its simplicity nevertheless makes it useful for learning object-oriented approaches.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top