in reply to how to remove an empty line from a xml file??

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

  • Comment on Re: how to remove an empty line from a xml file??

Replies are listed 'Best First'.
Re^2: how to remove an empty line from a xml file??
by ankit.tayal560 (Beadle) on Oct 01, 2016 at 08:34 UTC

    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 corion, that empty line is another child of $Item element I understood that but it is a XML::DOM::TEXT element. I don't know how to remove text nodes? don't write any code for me (y) but could you please tell me how to do that

        to remove a node I do : $parent_name=removeChild($node_name)but to remove text node what should I write in place of $node_name

      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?

        hey was doing a silly mistake. tried the above code . it works! thanks for the help

        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); my @list=$item->getChildNodes; foreach my $list(@list) { if($list->getNodeType==TEXT_NODE) { $item->removeChild($list); } } } } } } $doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8')); $doc->printToFile("C:/perl/perl_tests/xmlin2.xml"); $doc->dispose;