It's nothing fancy but here's a test script:
#!/usr/local/bin/perl
use bytes;
use strict;
use XML::Parser;
use XML::Writer;
use IO::File;
my $out_xml_file = "out.xml";
my $print_out_xml_file = "print_out.xml";
my $xmlFile = "in.xml";
my $parser = new XML::Parser();
$parser->setHandlers(
Start => \&StartHandler,
End => \&EndHandler,
Comment => \&CommentHandler,
Char => \&CharHandler,
Default => \&DefaultHandler,
);
my $print_out_file = new IO::File(">$print_out_xml_file");
my $out_file = new IO::File(">$out_xml_file");
binmode($out_file, ":encoding(utf-8)");
my $write = new XML::Writer(OUTPUT => $out_file,
DATA_INDENT => 2);
print $print_out_file qq(<?xml version="1.0" encoding="UTF-8"?>);
print $print_out_file "<root_out>\n";
$write->xmlDecl("UTF-8");
$write->startTag("root_out");
$parser->parsefile($xmlFile);
print $print_out_file "</root_out>";
$write->endTag("root_out");
$write->end();
$out_file->close();
$print_out_file->close();
## ---------------------##
## Handlers for XML Parser
##
sub StartHandler {
my ($p, $el) = @_;
print "start: $el\n";
print $print_out_file "<$el>\n";
$write->startTag($el);
}
sub EndHandler {
my ($p,$el) = @_;
print "end: $el\n";
print $print_out_file "</$el>\n";
$write->endTag($el);
}
sub CharHandler {
my ($p,$chr) = @_;
print "Char: $chr\n";
print $print_out_file "$chr";
$write->characters($chr);
}
sub CommentHandler {
my ($p,$com) = @_;
print "Comment found: $com\n";
}
sub DefaultHandler {
my($p,$str) = @_;
print "Default found: $str\n";
}
...and the "in.xml" file (I'm not sure the utf-8 in the test1 element will be correct after copy-n-paste):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test1></test1>
<test2>—</test2>
</root>
|