use strict; use warnings; use Data::Dumper; use XML::DOM; my $parser=new XML::DOM::Parser; my $doc=$parser->parsefile('C:\perl\perl_tests\xmlin2.xml') or die$!; my $root=$doc->getDocumentElement(); my @address=$root->getElementsByTagName("address"); foreach my $address(@address) { if($address->getAttribute("name") eq "Joey") { if(my $item=$address->getElementsByTagName("item")->item(0)) { if(my $data=$item->getElementsByTagName("data")->item(0)) { $item->removeChild($data); } } } } $doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8')); $doc->printToFile("C:/perl/perl_tests/xmlin2.xml"); $doc->dispose; XML FILE ORIGINAL : <?xml version="1.0" encoding="UTF-8"?> <config logdir="var/log/foo/" debugfile="tmp/foo.debug"> <server name="sahara" osname="solaris" osversion="2.6"> <address name="ankit" id="70888"/> <address name="Joey" id="67890" flags="4"> <item used="1" order="0"> <data typeid="4"/> </item> </address> </server> <server name="gobi" osname="irix" osversion="6.5"> <address name="anshul" id="70689"/> </server> <server name="kalahari" osname="linus" osversion="2.0.34"> <address name="raghu" id="45678"/> <address name="lucky" id="67895"/> </server> </config> XML FILE AFTER MODIFICATION: <?xml version="1.0" encoding="UTF-8"?> <config logdir="var/log/foo/" debugfile="tmp/foo.debug"> <server name="sahara" osname="solaris" osversion="2.6"> <address name="ankit" id="70888"/> <address name="Joey" id="67890" flags="4"> <item used="1" order="0"> </item> </address> </server> <server name="gobi" osname="irix" osversion="6.5"> <address name="anshul" id="70689"/> </server> <server name="kalahari" osname="linus" osversion="2.0.34"> <address name="raghu" id="45678"/> <address name="lucky" id="67895"/> </server> </config> IDEAL MODIFIED XML FILE SHOULD BE : <?xml version="1.0" encoding="UTF-8"?> <config logdir="var/log/foo/" debugfile="tmp/foo.debug"> <server name="sahara" osname="solaris" osversion="2.6"> <address name="ankit" id="70888"/> <address name="Joey" id="67890" flags="4"> <item used="1" order="0"> </item> </address> </server> <server name="gobi" osname="irix" osversion="6.5"> <address name="anshul" id="70689"/> </server> <server name="kalahari" osname="linus" osversion="2.0.34"> <address name="raghu" id="45678"/> <address name="lucky" id="67895"/> </server> </config>

I need to remove the empty line which is created due to deletion of data element in the xml file to achieve the ideal modified xml file. how can I do that? any suggestions?


In reply to how to remove an empty line from a xml file?? by ankit.tayal560

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.