Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Append in XML using perl

by antovinraj (Initiate)
on Jun 07, 2006 at 09:57 UTC ( [id://553997]=perlquestion: print w/replies, xml ) Need Help??

antovinraj has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Append in XML using perl
by gellyfish (Monsignor) on Jun 07, 2006 at 10:19 UTC

    It would probably help if you were to give us a tiny example of what you are trying to do, but using XML::DOM adding and deleting nodes is really quite easy:

    use XML::DOM; my $xml =<<EOXML; <Stuff> <Things> <Thing /> <Thing /> </Things> <BadThing /> </Stuff> EOXML my $parser = new XML::DOM::Parser; my $doc = $parser->parse($xml); my $things = $doc->getElementsByTagName ("Things")->[0]; my $newthing = $doc->createElement('Thing'); $things->appendChild($newthing); my $badthings = $doc->getElementsByTagName ("BadThing"); foreach $badthing ( @{$badthings} ) { $doc->getDocumentElement()->removeChild($badthing); } print $doc->toString();
    The above adds a <Thing /> within the <Things /> node and removes all the <BadThings />

    /J\

Editing in xml
by antovinraj (Initiate) on Jun 07, 2006 at 10:55 UTC
    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.

      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
      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$..$/
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://553997]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found