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\


In reply to Re: Editing in xml by gellyfish
in thread Append in XML using perl by antovinraj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.