[Buffalo-pm] XML File Parsing And Manipulation...

DANIEL MAGNUSZEWSKI dmagnuszewski at mandtbank.com
Thu Dec 29 07:52:32 PST 2005


Andrew,

Thanks. This looks like a pretty interesting and clean solution -
especially as the parsing needs increase. 


>>> "Andrew Bruno" <aebruno2 at cse.Buffalo.EDU> 12/29/05 1:42 AM >>>

Hi Dan,

I would check out XSLT. I know this is the un-perl way to do it but
XSLT
is a language built for transforming XML documents and XPath/XSLT
provide
some nice ways of manipulating XML. Attached is a quick XSLT that seems
to
do the same thing as your perl program. You will need an XSLT processor
to
run it and can use the perl module XML::LibXSLT. There is also a great
command line utility called xsltproc (part of libxml) which is included
on
most Linux distros. If you have it just run:

  $ xsltproc remove-ds1.xslt example.xml

This will print out the new XML file with the elements removed to
stdout.
It may be overkill for your problem but it might be worth looking into
if
your going to be doing quite a bit of XML processing.

I would also check out XML::LibXML and XML::LibXSLT which are the perl
bindings into the GNOME libxml libraries. It's always a good idea to
use
some kind of XML processing library rather than regex's when it comes
to
parsing XML. The libraries will usually help out dealing with
well-formedness, escaping entities, encodings, and all the subtleties
of
XML. Here is some example perl code that will transform your XML file
using the attached XSLT:

use strict;
use XML::LibXML;
use XML::LibXSLT;

# Parse the XML file
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file("example.xml");

# Transform with XSLT
my $xslt = XML::LibXSLT->new();
my $style_doc = $parser->parse_file("remove-ds1.xslt");
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($doc);
$stylesheet->output_file($results, "example.xml.new");






More information about the Buffalo-pm mailing list