XML technologies / XML / Perl & XML / Tree-oriented processing / XML::Simple
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
The simplest tree model is in Grant McLean’s module XML::Simple, designed for simple files such as configuration. No XML or parser expertise is needed: arrays and hashes represent the document, familiar territory for Perl programmers.
Here is a simple configuration file:
Example: Program configuration
<konfiguration>
<zeichensatz role="default">
<name>Times New Roman</name>
<groesse>14</groesse>
</zeichensatz>
<fenster>
<hoehe>352</hoehe>
<breite>417</breite>
<position_x>100</position_x>
<position_y>120</position_y>
</fenster>
</konfiguration> XML::Simple makes access remarkably easy. The next example reads font information.
Example: Reading font information
use XML::Simple;
my $simple = XML::Simple->new(); # Initialisierung des Objekts
my $tree = $simple->XMLin( './data.xml' ); # Lies das Dokument und speichere es in $tree
# Zugriff auf die Daten in $tree
print "Der Anwender wünscht den Zeichensatz " . $tree->{ zeichensatz }->{ name } .
" mit einer Größe von " . $tree->{ zeichensatz }->{ groesse } . " Punkten.\n";Initialize an object of class XML::Simple, then call its method
XMLin( ). It returns a reference to the root of a hierarchical tree of hash references. Keys are element names; values are strings or hashes representing children. Access to document parts is clear and simple.
To illustrate, print the tree with Data::Dumper, a module visualizing data structures as Perl code. Add these lines:
use Data::Dumper;
print Dumper( $tree );The output is:
$tree = {
'zeichensatz' => {
'groesse' => '14',
'name' => 'Times New Roman',
'role' => 'default'
},
'fenster' => {
'position_x' => '100',
'position_y' => '120',
'hoehe' => '352',
'breite' => '417'
}
}; The variable $tree represents the root element, <konfiguration>. Its hash references children <zeichensatz> and <fenster>, retrieved by name. These hashes represent the third level, whose string values reproduce the input text. The entire document consists of hash references.
This was a simple example. The success of XML::Simple depends largely on assuming simple XML: no siblings share names in the input, which hashes alone would struggle to represent.
Fortunately, XML::Simple handles that too, grouping same-named children in a list. Modify the input as follows:
Example: A more complex configuration
<konfiguration>
<zeichensatz role="console">
<groesse>9</groesse>
<fname>Courier</fname>
</zeichensatz>
<zeichensatz role="default">
<fname>Times New Roman</fname>
<groesse>14</groesse>
</zeichensatz>
<zeichensatz role="titles">
<groesse>10</groesse>
<fname>Helvetica</fname>
</zeichensatz>
</konfiguration> This time XML::Simple faces a curveball: three successive elements <zeichensatz>. A data dump shows its response.
$tree = {
'zeichensatz' => [
{
'fname' => 'Courier',
'groesse' => '9',
'role' => 'console'
},
{
'fname' => 'Times New Roman',
'groesse' => '14',
'role' => 'default'
},
{
'fname' => 'Helvetica',
'groesse' => '10',
'role' => 'titles'
}
]
}; The key zeichensatz now holds an array reference, containing hash references for the elements <zeichensatz>. To find a particular font, iterate until it appears. Same-named siblings are handled.
The new input also adds attributes. They enter the structure like child elements. Attribute/element name collisions use the same list solution. If merging attributes and elements is acceptable, this is convenient.
What about writing? XML::Simple provides the method XMLout( ) to output XML. Modify a previously read structure or construct a new one; these structures are simple enough to build yourself.
Our conclusion? XML::Simple handles simple XML well but struggles with complexity, for example elements mixing text and children, called “mixed content.” Input supports only node types for elements, attributes and text, excluding processing instructions and sections CDATA. Hash keys lose order, potentially rearranging elements. If that is acceptable, use XML::Simple. It does its job wonderfully, with no simpler way to handle markup and data structures.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is 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