Re: XML::XPath createNode with the same name??

Michael Ludwig <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <20100406200815.GA3464@wladimir>
Brian schrieb am 06.04.2010 um 13:57:45 (-0400):
> Using XML::Xpath, I would like to be able to add multiple child nodes
> to the <recipientList> node, all called <recipient>

I've never used XML::XPath, but here's a solution using XML::LibXML:

         \,,,/
         (o o)
-----oOOo-(_)-oOOo-----
use common::sense;
use XML::LibXML;

my @rcpts = qw/
[email protected]
[email protected]
[email protected]
[email protected]/;

my $xml_file = shift or die 'Datei!';
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file( $xml_file );
my $xpc = XML::LibXML::XPathContext->new( $doc );
my @nodes = $xpc->findnodes( '( //recipientList )[1]' );
for my $rl ( @nodes ) { # there is only one
  for ( @rcpts ) {
    my $rcpt = $doc->createElement( 'recipient' );
    $rcpt->appendTextNode( $_ );
    $rl->appendChild( $rcpt );
  }
}
print $doc->toString;

Simply pass the filename like this:

perl xpath.pl your-doc.xml

-- 
Michael Ludwig
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.