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 | |
by ankit.tayal560 (Beadle) on Oct 01, 2016 at 08:34 UTC | |
by Corion (Patriarch) on Oct 01, 2016 at 08:46 UTC | |
by ankit.tayal560 (Beadle) on Oct 01, 2016 at 09:08 UTC | |
by Corion (Patriarch) on Oct 01, 2016 at 09:13 UTC | |
by beech (Parson) on Oct 01, 2016 at 09:00 UTC | |
by ankit.tayal560 (Beadle) on Oct 01, 2016 at 09:29 UTC |