How to remove an empty line which is created when i deleted a element from my xml file?

Discussion in 'Perl' started by Ankit Tayal, Oct 1, 2016.

  1. Ankit Tayal

    Ankit Tayal

    Joined:
    Oct 1, 2016
    Messages:
    1
    Likes Received:
    0
    MY CODE IS :


    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;





    I use XML:: DOM to parse xml file and do modifications in it . when I deleted an element named "data" it created an empty line (although data element got deleted) but I want to remove this empty line from my xml document. how can I do it ? any suggestions or solutions are welcome?
     
    Ankit Tayal, Oct 1, 2016
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.