ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
Re: how to add a node in XML::DOM
by Romeo Juncu other posts by this author
Jan 26 2001 10:20PM messages near this date
view in the new Beta List Site
how to add a node in XML::DOM | very newbie to perl-xml
Something like this worked very fine to me:


#! /usr/bin/perl

use XML::DOM;

my $xp=new XML::DOM::Parser;
my $doc=$xp-> parsefile("d:\\Projects\\Seline-XMLS\\Copie de Sites.xml");
my $docFrom=$xp-> parsefile("d:\\Sites.xml");

my $nodes=$docFrom-> getElementsByTagName("codeLabo");
my $snode;
for my $i (0..$nodes-> getLength-1) {
 my $node=$nodes-> item($i);
 if ($node-> getFirstChild->getData eq 'TEST') {
  $snode=$node-> getParentNode->cloneNode(true);
  last;
 }
}

$snode-> setOwnerDocument($doc);
$sitesnode=$doc-> getElementsByTagName("sites")->item(0);
$sitesnode-> appendChild($snode);

$doc-> printToFile("d:\\Copie de Sites.xml");


I've copied a node site with it's child <codeLabo> TEST</codeLabo> from
"Site.xml" to "Copie de Site.xml"
XML data is:
<sites xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./schemas/Sites.xsd" nomGroupement="ARMONI"> 
 <site maitre="false" test="test value"> 
  <codeLabo> BELLE</codeLabo>

<dossierConfigurationMonitoring> \\snt1\commun\serd\seline\services\conf</dos
sierConfigurationMonitoring> 
  <centreTri nomMachine="Snt1" portTcp="100"/> 
 </site> 
...............................
................................
 <site maitre="false"> 
  <codeLabo> TEST</codeLabo>
  <dossierConfigurationMonitoring> ./conf</dossierConfigurationMonitoring>
  <centreTri nomMachine="rjuncu" portTcp="100"/> 
 </site> 
</sites> 


----- Original Message -----
From: John Quigley <jquigley@[...].org> 
To: Perl XML mailing list (ActiveState) <perl-xml@[...].com> 
Sent: Thursday, January 25, 2001 11:42 PM
Subject: how to add a node in XML::DOM


>  Greetings all,
> 
>  I'm having trouble adding a node to a document tree. The situation is
>  briefly the following:
> 
>  I've got this one DOM object/tree that has data in it.
>  I've got this other DOM object/tree that contains barebones xml and
>  some junk data...it's basically a template for the type of xml I'm working
>  with.
> 
>  I need to add nodes to the DOM that has good data in it. I'm trying to
>  achieve this by cloning a "junk" node from the template xml DOM and then
>  adding that node to the DOM with good data in it.
> 
>  Methods attempted but not successful: setOwnerDocument, setParentNode, and
>  appendChild.
> 
>  Any thoughts on a clean way to do this? Any documentation I can be pointed
>  to?
> 
>  Thanks in advance,
> 
>          -John Quigley
> 
> 
>  _______________________________________________
>  Perl-XML mailing list
>  Perl-XML@[...].com
>  http://listserv.ActiveState.com/mailman/listinfo/perl-xml

_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
http://listserv.ActiveState.com/mailman/listinfo/perl-xml
Thread:
John Quigley
Romeo Juncu

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved