Styric has asked for the wisdom of the Perl Monks concerning the following question:
So, it turns out inserting a new node into an existing xml list is not as easy as I thought. I'm sure I'm missing something but been tinkering with this for quite some time now so I ask you for some guidance.
In short, I'm using a file as a base model which is being read in that looks similar to this.
Say I want to add something to the 1st floor like this. <node> <label>10.1.1.1</label> </node> I've gone through many different variations so I'll post one of them as the closest I can come is appending it to the end and not to the floor I want.<top> <nodes> <node> <label>Office</label> <node> <label>1st Floor</label> </node> <node> <label>2nd Floor</label> </node> <node> <nodes> </top>
Can someone please help?#!/usr/bin/perl use XML::LibXML; $filename = "Office.xml"; my $floor = '1st Floor'; my $bldg = "Building A"; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("$filename") or die; my $root = $doc->getDocumentElement(); my $parent = $doc->documentElement; my $newnode = $doc->documentElement; my $query = "//node[label = '$bldg']/node[label = '$floor']/label/tex +t()"; if(my($node) = $doc->findnodes($query)) { my $new_element= $doc->createElement("IP"); $new_element->appendText('10.1.1.1'); $newnode = $parent->addSibling($new_element); print $newnode -> toString; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: libxml - insert node
by tobyink (Canon) on Feb 14, 2012 at 09:46 UTC | |
by Styric (Initiate) on Feb 15, 2012 at 06:27 UTC | |
|
Re: libxml - insert node
by choroba (Cardinal) on Feb 14, 2012 at 11:24 UTC | |
|
Re: libxml - insert node
by ikegami (Patriarch) on Feb 14, 2012 at 18:28 UTC |