RE: Using the SAX interface

"Ben Hobbs" <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
For something so simple why not make an XSLT document to do the 
transformation?

I can't find anything wrong with what you've written to cause it to 
crash.  Another, much simpler, XML parser I use is XML::Twig.  It is a 
SAX/DOM hybrid.  It'll make this much easier for you I think.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Michael
Ludwig
Sent: Monday, June 02, 2008 3:33 AM
To: [email protected]
Subject: Using the SAX interface

Given (1) a simple document:

<Dok>
   <E>1</E>
   <E>2</E>
   <E>3</E>
   <E>4</E>
   <E>5</E>
   <E>6</E>
   <E>7</E>
   <E>8</E>
   <E>9</E>
   <E>10</E>
   <E>11</E>
   <E>12</E>
   <E>13</E>
</Dok>

(2) a simple requirement to transform it via SAX (imaginary giant
input document) to the following:

<Dok>
   <Gr>
     <E>1</E>
     <E>2</E>
     <E>3</E>
     <E>4</E>
     <E>5</E>
     <E>6</E>
   </Gr>
   <Gr>
     <E>6</E>
     <E>7</E>
     <E>8</E>
     <E>9</E>
     <E>10</E>
     <E>11</E>
   </Gr>
   <Gr>
     <E>11</E>
     <E>12</E>
     <E>13</E>
   </Gr>
</Dok>

How would I proceed in Perl?

I seem to have very basic beginner problems. Here's what I have so far:

use strict;
use warnings;
use XML::SAX::ParserFactory;
use XML::SAX::Writer;

my $xmlfile = shift or die 'Zu parsendes Dokument uebergeben!';
my $filter = Gurke->new(Handler => XML::SAX::Writer->new);
$XML::SAX::ParserPackage = 'XML::LibXML::SAX';
my $parser = XML::SAX::ParserFactory->parser(Handler => $filter);

eval { $parser->parse_file($xmlfile) };
die "parse_file($xmlfile) : $@" if $@;

package Gurke;
use base 'XML::SAX::Base';

sub start_element {
     my($self, $data) = @_;
     $self->SUPER::start_element($data);
}

The output I get when processing the XML document shown above is:

<?xml version="1.0"?>
<Dok>
   <E>1</E>
   <E>2</E>
   <E>3</E>
   <E>4</E>
   <E>5</E>
   <E>6</E>
   <E>7</E>
   <E>8</E>
   <E>9</E>
   <E>10</E>
   <E>11</E>
   <E>12</E>
   <E>13</E>
</Dok>IO::Handle=IO(0x81532f4)

What's that stringified object reference doing there at the end?

When I change start_element() to only print the element names:

sub start_element {
     my($self, $data) = @_;
     print $data->{Name}, "\n";
}

I get the following output (STDOUT and STDERR combined):

<?xml version="1.0"?>
Dok
E

   1</E>E

parse_file(sax.xml) : Trying to pop context without push context at 
/usr/share/perl5/XML/NamespaceSupport.pm line 79. at 
/usr/local/lib/perl/5.8.8/XML/LibXML/SAX.pm line 64
  at sax.pl line 12
   2</E>

Searching for "Trying to pop context without push context" led me to:

http://osdir.com/ml/lang.perl.xml/2003-10/msg00029.html

But I don't have two documents here, I have only one.

How is this SAX API supposed to be used?

How would I go about solving the task outlined above?

Michael Ludwig
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.