XML technologies / XML / Perl & XML / SAX / Processing non-XML data
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
The filter example used a file containing an XML document as input, demonstrating one of the many uses of SAX. Another excellent use is reading data from a driver: a program generating SAX events from input in a completely different format. These events can be processed exactly like those from a SAX parser. This lets a processor designed for XML handle other formats. The SAX event stream abstracts data and markup, so we need not deal with those details. Adapting the program to other files or drivers is then relatively easy.
To see a driver in action, we will use the module XML::SAXDriver::Excel by Ilya Sterin to convert a Microsoft Excel spreadsheet into XML. This demonstrates stream processing like a Pipeline to obtain the desired format. An object of type Spreadsheet::ParseExcel reads the file and generates a data stream. An object of type XML::SAXDriver::Excel converts it into SAX events, which our program outputs.
For testing, we use this Excel spreadsheet:
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
The SAX driver creates elements and supplies their names as handler arguments. We simply output them as received to examine the structure the driver produces. The following example does exactly this.
Example: Reading an Excel spreadsheet
use XML::SAXDriver::Excel;
# Lies den Namen der zu verarbeitenden Datei
die( "Eine Eingabedatei muß angegeben werden" ) unless( @ARGV );
my $file = shift @ARGV;
print "Datei $file wird gelesen...\n";
# Initialisierung des Parsers
my $handler = new Excel_SAX_Handler;
my %props = ( Source => { SystemId => $file },
Handler => $handler );
my $driver = XML::SAXDriver::Excel->new( %props );
# Start des Parsers
$driver->parse( %props );
# Um SAX-Events zu erhalten, brauchen wir ein Package,
# das die Handlermethoden enthält.
package Excel_SAX_Handler;
# Initialisierung des Objekts
sub new {
my $type = shift;
my $self = {@_};
return bless( $self, $type );
}
# Erzeuge das Wurzelelement
sub start_document {
print "<?xml version='1.0' encoding='iso-8859-1'?>\n<doc>\n";
}
# Beende das Wurzelelement
sub end_document {
print "</doc>\n";
}
# Behandlung von Textdaten
sub characters {
my( $self, $properties ) = @_;
my $data = $properties->{'Data'};
print $data if defined($data);
}
# Start eines neuen Elements, wir geben nur das Start-Tag aus
sub start_element {
my( $self, $properties ) = @_;
my $name = $properties->{'Name'};
print "<$name>";
}
# Ende eines Elements
sub end_element {
my( $self, $properties ) = @_;
my $name = $properties->{'Name'};
print "</$name>";
}The handlers resemble those in the previous SAX example; only what we do with them has changed. Let us see what running the program produces from our test spreadsheet:
<?xml version='1.0' encoding='ISO-8859-1'?>
<doc>
<records>
<record>
<column1>Fußbälle</column1>
<column2>55</column2>
</record>
<record>
<column1>Tennisbälle</column1>
<column2>33</column2>
</record>
<record>
<column1>Pingpongbälle</column1>
<column2>12</column2>
</record>
<record>
<column1>Rauschende Bälle</column1>
<column2>77</column2>
</record>
<record>
Use of uninitialized value in print at conv line 39.
<column1></column1>
Use of uninitialized value in print at conv line 39.
<column2></column2>
</record>
</records></doc> The driver did most of the work, creating elements and structuring data. We only output the data delivered through handler calls. It wraps the document in an element <records>, so our own root element <doc> was unnecessary. In the next version, we will change the handlers start_document( ) and end_document( ) to produce no output. Each spreadsheet row is wrapped in a <record> element, with the two columns distinguished by their names <column1> and <column2>. Not bad for a start.
With minimal effort, SAX let us convert one format into another. The driver nearly automates the conversion while leaving enough flexibility to control events. We can remove unwanted data such as empty rows, rename elements, or perform more complex processing such as summing values or sorting rows.
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