[sf-perl] XML Parsing question
Francisco Obispo
fobispo at isc.org
Sat Mar 5 10:23:00 PST 2011
That would work ;-)
Just be aware that because you are appending to the current document, if you encounter thousands of xml files, you might run out of memory before finishing.
This is why I recommend XML::Writer, so you can free the memory as soon as you're done processing the file.
Best regards.
Francisco
On Mar 5, 2011, at 7:29 AM, Bill Moseley wrote:
>
>
> 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
Francisco Obispo
Hosted@ Programme Manager
email: fobispo at isc.org
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
Key fingerprint = 532F 84EB 06B4 3806 D5FA 09C6 463E 614E B38D B1BE
More information about the SanFrancisco-pm
mailing list