[sf-perl] XML Parsing question

Bill Moseley moseley at hank.org
Sat Mar 5 07:29:30 PST 2011

On Fri, Mar 4, 2011 at 9:57 PM, Francisco Obispo <fobispo at isc.org> wrote:

> Well, if look at my code, I'm not reading the CDATA section directly, I'm
> just asking for the value of the 'content' tag, if it has a CDATA element in
> it, well, XML::LibXML will take care of it.
Oh, right.  I was confused by your explicit $writer->cdata call.  I could
see you had knowledge that there was a <content> tag and was CDATA, where I
needed a way to preserve the CDATA sections without knowing if any child of
<result> had a CDATA section ahead of time.

So, what what I ended up with is this:

use strict;
use XML::LibXML;
use Data::Dumper;

my $parser = XML::LibXML->new;
my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
my $root = $doc->createElement( 'testResults' );
$doc->setDocumentElement( $root );

foreach my $file (@ARGV) {
   my $doc = $parser->parse_file($file);

   $root->appendChild( $_ ) for $doc->getElementsByTagName( 'result' );


print $doc->toString;

Pretty simple.

Thanks very much for your help, Francisco.

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20110305/2089aa79/attachment.html>

More information about the SanFrancisco-pm mailing list