Hi Monks,
I'm trying to use XML::SAX to modify parts of an XHTML document, however all my attempts have failed.
Here is what I'm trying to do:
#!/usr/bin/perl
package MyHandler;
use strict;
use warnings;
use base qw(XML::SAX::Base);
use Data::Dumper;
sub start_element {
my $self = shift;
my $data = shift;
if( $data->{LocalName} eq 'span') {
$data->{LocalName} = 'naps';
}
$self->SUPER::start_element($data); # GOOD (and easy) !
#print Dumper($data);
}
1;
#============================
#Main programm
#============================
use strict;
use warnings;
use XML::SAX::ParserFactory;
use XML::SAX::Writer;
my $out;
my $o = XML::SAX::Writer->new( Output => \$out );
my $h = MyHandler->new( Handler => $o );
my $p = XML::SAX::ParserFactory->parser(Handler => $h);
my $data;
{ local undef $/ }; $data = <DATA>;
$p->parse_string( $data );
print $out;
__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wi
+cket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<body>
<wicket:panel>
<form wicket:id="mvpForm">
<span>Edit Information: </span>
<input type="checkbox" wicket:id="editForm"/>
<span>Name: </span>
<span wicket:id="name"></span>
<input type="text" wicket:id="nameEdit"/>
<span>Last Name: </span>
<span wicket:id="lastName"></span>
<input type="text" wicket:id="lastNameEdit"/>
<span>DOB: </span>
<span wicket:id="dob"></span>
<input type="text" wicket:id="dobEdit"/>
<span>Occupation: </span>
<span wicket:id="occupation"></span>
<input type="text" wicket:id="occupationEdit"/>
<span>Gender: </span>
<span wicket:id="gender"></span>
<span wicket:id="genderEdit"/>
<input type="submit" wicket:id="submit"/>
</form>
</wicket:panel>
</body>
</html>
The basic idea is to change every "span" to a "naps" and write the resulting modified XML to STDOUT.
Also, it'd be nice to see if its possible to merge xml chunks using SAX, in other words, if I found a particular element that gets expanded to something else, how can I merge it with the output going to STDOUT?
E.g.
From:
<xmltag>
<expandable/>
</xmltag>
To:
<xmltag>
<expanded>
This is an expanded element
</expanded>
</xmltag>
Thanks.
PS
I'm crossposting this on stackoverflow
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|