in reply to Manipulate xml with libxml
XML is very particular in that an element is an element, it already exists. Your were on the right track with cloneNode, you just needed to combine that with the appendSibling.#!/usr/bin/perl -w -s use strict; use XML::LibXML; my $it = shift || 1; my $nn = shift || ''; print qq($it\n); my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("model.xml") or die "cannot open xml fil +e: $!"; my $node = $doc->getDocumentElement(); my @dc = $node->getElementsByTagName('fixe'); foreach my $descr (@dc) { foreach my $elt ($descr->childNodes()) { next unless $elt->nodeType() == 1; #to avoid text nodes if ( $elt->nodeName eq $nn ) { $descr->addSibling($elt->cloneNode) foreach ( 1 .. $it ); } else { $descr->addSibling($elt); } } my $parent = $descr->parentNode; $parent->removeChild($descr); } print $doc->toString();
Edit by castaway: Closed small tag in signature
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Manipulate xml with libxml
by Anonymous Monk on Jun 08, 2005 at 09:20 UTC | |
by BaldPenguin (Friar) on Jun 08, 2005 at 16:24 UTC | |
by mirod (Canon) on Jun 08, 2005 at 16:34 UTC | |
|
Re^2: Manipulate xml with libxml
by Anonymous Monk on Jun 08, 2005 at 12:13 UTC |