Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Processing Two XML Files in Parallel

by ikegami (Patriarch)
on Jul 21, 2011 at 22:19 UTC ( [id://916009]=note: print w/replies, xml ) Need Help??


in reply to Processing Two XML Files in Parallel

use strict; use warnings; use XML::LibXML qw( ); die "Usage" if @ARGV != 3; my $parser = XML::LibXML->new(); my @counts; { my $doc = $parser->parse_file($ARGV[1]); my $root = $doc->documentElement(); @counts = map $_->textContent, $root->findnodes('elem'); } { my $doc = $parser->parse_file($ARGV[0]); my $root = $doc->documentElement(); for my $node ($root->findnodes('elem')) { die "Not enough counts" if !@counts; $node->appendText( $node->textContent() x (shift(@counts) - 1) ); } print $doc->toFile($ARGV[2]); } die "Too many counts" if @counts;

Or if counts of 0 are acceptable:

my $new_text = $node->textContent() x shift(@counts); $node->removeChild($_) for $node->findnodes('text()'); $node->appendText($new_text);

Tested.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://916009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-18 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found