92sai has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have used the below code to make modifications in xml file. but the modified sheet has unnecessary new lines(many lines) or white spaces. How do i remove the extra white lines that are in the xml file.
my $a_type = "alpha"; my $type=$a_type; if($type eq "alpha"){ $file='beta.xml'; } my $twig= XML::Twig->new( pretty_print => 'indented', twig_roots => { name => sub{ my ($p, $name) = @_; $name->set_text( "sai" ); $name->print; $name->purge; return; }, } twig_print_outside_roots => 1, ); $twig->parsefile_inplace( $file, 'bak.*' ); $twig->flush;
my input is
<?xml version='1.0' encoding='UTF-8'?> <project> <actions/> <name>Ram</name> <daysToKeep>10</daysToKeep>
and the output i got is
<?xml version='1.0' encoding='UTF-8'?> <project> <actions/> <name>Sai</name> <daysToKeep>10</daysToKeep>
you can see the extra lines that i donot want.. in the output. The desired output is
<?xml version='1.0' encoding='UTF-8'?> <project> <actions/> <name>Sai</name> <daysToKeep>10</daysToKeep>

Replies are listed 'Best First'.
Re: How to remove spaces after modifying an xml sheet.
by thanos1983 (Parson) on Jun 18, 2015 at 09:48 UTC

    Hello 92sai

    Please provide us with sample of input, sample of output and also desired output. We can answer your question without this data. Have also considered alternative modules? XML::Simple, XML::Parser etc.

    Although that I have never used the XML::Twig I think (without been able to verify) your error comes from: (purge). From documentation: You can also purge it if you don't need to output it (if you are just extracting some data from the document for example). The handler will be called again once the next relevant element has been parsed.

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      i have updated the question with desired output
      I have never used the XML::Twig I think (without been able to verify)

      Sorry but it seems purge has no influence on the problem.

      Also, in regards to XML::Simple see e.g. here and the thread it links to.

Re: How to remove spaces after modifying an xml sheet.
by Anonymous Monk on Jun 18, 2015 at 09:54 UTC

    The code you posted has errors, and you didn't post any sample input, which makes it difficult to reproduce your problem.

    I think part of the reason you're having trouble is hinted at in XML::Twig:

    Pretty printing (at least using the 'indented' style) is hard to get right! Only elements that belong to the document will be properly indented. Printing elements that do not belong to the twig makes it impossible for XML::Twig to figure out their depth, and thus their indentation level.

    Removing the pretty_print option seems to work for me; you could also try replacing it with keep_spaces => 1.

    Otherwise, you might consider doing a second pass on the document to re-indent it after you've modified it; I'm not sure if XML::Twig will allow you to do it in a single pass, perhaps someone more versed in that module knows better.

      removing the pretty_print option is printing the whole xml on the command line, which i dont need.
        removing the pretty_print option is printing the whole xml on the command line

        I doubt that; at least I can't reproduce it with the code sample you gave. Are you sure that's the only change you made to your code?