in reply to Append in XML using perl

My xml file is
<?xml version="1.0" encoding="windows-1252"?> <IDList><Details><id>Trial ID</id><name>Mooza </name></Details><Details><id>Trial ID</id><name>bin_salim99 </name></Details><Details><id>Trial ID</id><name>dino88 </name></Details><Details><id>Trial ID</id><name>al3neeeeed </name></Details><Details><id>TrialID</id><name>Do0oDa</name></Details +></IDList>
   In this I want to add a new node in the last or want to edit the note or delete the node.

   I have read the values from the text file and write it in to xml. the code is
use XML::Writer; use XML::XPath; use XML::XPath::Node; use IO::File; open(NEW, "<<../main/idno.txt"); @id = <NEW>; close (NEW); my $output = new IO::File(">data9.xml"); my $doc = XML::Writer->new(OUTPUT => $output); $doc->xmlDecl("windows-1252"); $doc->startTag("IDList"); foreach (@id) { ($actid, $name) = split(/:/, $_); $doc->startTag( "Details" ); $doc->dataElement( id => $actid); $doc->dataElement( name => $name); $doc->endTag("Details"); } $doc->endTag("IDList"); # close IDList $doc->end(); # final checks $output->close();
Now I want to add a new child in the xml file or edit a node or delete the node. How i can do it and give some suggestions for that. If have any code send it.

Replies are listed 'Best First'.
Re: Editing in xml
by gellyfish (Monsignor) on Jun 07, 2006 at 14:00 UTC

    Manipulating an XML document with XML::DOM is quite easy, if not a little verbose. An example that alters, deletes and adds a new child to your document:

    use XML::DOM; my $xml =<<EOXML; <?xml version="1.0" encoding="windows-1252"?> <IDList><Details><id>Trial ID</id><name>Mooza </name></Details><Details><id>Trial ID</id><name>bin_salim99 </name></Details><Details><id>Trial ID</id><name>dino88 </name></Details><Details><id>Trial ID</id><name>al3neeeeed </name></Details><Details><id>TrialID</id><name>Do0oDa</name> </Details></IDList> EOXML my $parser = new XML::DOM::Parser; my $doc = $parser->parse($xml); my $root = $doc->getDocumentElement(); foreach my $detail (@{$root->getChildNodes()}) { my $name_el = $detail->getElementsByTagName('name')->[0]; my $name = $name_el->getFirstChild(); if ($name->toString() =~ /Mooza/) { my $new = $doc->createTextNode('Foobar'); $name_el->replaceChild($new, $name); } elsif ( $name->toString() =~ /Do0oDa/ ) { $root->removeChild($detail); } } my $newdet = $doc->createElement('Details'); my $new_id = $doc->createElement('id'); my $new_id_val = $doc->createTextNode('Test ID'); $new_id->appendChild($new_id_val); $newdet->appendChild($new_id); my $new_name = $doc->createElement('name'); my $new_name_val = $doc->createTextNode('Yargle'); $new_name->appendChild($new_name_val); $newdet->appendChild($new_name); $root->appendChild($newdet); print $doc->toString();
    In the real world you probably would parcel the separate parts of this up in subroutines I guess.

    /J\

      But it is not appended in xml file but is it showin while the program is running in the command prompt. Give some comments

        Er quite, this is just an example of manipulating the XML, you can open your XML file with XML::DOM using parsefile instead of parse and writing back to the file after you have manipulating it is a trivial matter of opening a file for writing and printing $doc->toString() to it.

        /J\

Re: Editing in xml
by liverpole (Monsignor) on Jun 07, 2006 at 10:59 UTC
    Hi antovinraj,

    Please don't start new posts about a question you've already asked.  It's better to continue your previous post instead.

    You can always add an update section if you don't want to start a new comment.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/