ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: how to remove an empty line from a xml file??
by beech (Parson) on Oct 01, 2016 at 07:04 UTC

    Hi

    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?

    The empty line is just another child XML::DOM::Node , no not an element, but XML::DOM::Text node, a child of $item, a sibling to $data,

    So ->getChildNodes and ->removeChild as you require

      understood your point! thanks but I am unable to implement it in my script. could you please provide me with a prototype or an example script for my problem ???

        Hi,

        understood your point! thanks but I am unable to implement it in my script. could you please provide me with a prototype or an example script for my problem ???

        Can I see what you tried?