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

Hello, I am a very basic user of PERL, and Im trying to place a line on the bottom of an XML file to the top of the same XML file. My XML file reads as:

<testcase Classname="combination" name="combination001" time="2.558220 +14808655"/> <testcase Classname="combination" name="combination002" time="2.420861 +9594574"> <failure message= "test failure"> </failure> </testcase> <testcase Classname="combination" name="combination003" time="2.807042 +12188721"> <failure message= "test failure"> </failure> </testcase> <testcase Classname="combination" name="combination004" time="3.021811 +00845337"> <failure message= "test failure"> </failure> </testcase>
<testsuite errors="0" failures="5" name="combination" skips="0" tests="5" time="14.2159960269928">

I am trying to place "<testsuite errors...> line on the top.

</testsuite> <--- stays put on the bottom

any tips will be appreciated

Replies are listed 'Best First'.
Re: placing an existing line from my XML file to the top of the file
by choroba (Cardinal) on Aug 18, 2015 at 23:51 UTC
    Using XML::XSH2, a wrapper around XML::LibXML:
    open file.xml ; move /code/testsuite prepend /code ; save :b ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: placing an existing line from my XML file to the top of the file
by GrandFather (Saint) on Aug 19, 2015 at 06:07 UTC

    Why? In the sample XML you provide that doesn't seem a sensible thing to do as it would break the XML. What is the problem you are trying to solve by moving that line?

    Premature optimization is the root of all job security

      Thanks for reply. I am trying to match an XML schema that gets created by a different Test Infrastructure.

        I am trying to match an XML schema that gets created by a different Test Infrastructure.

        Then better don't think in terms of lines. XML has no concept of lines. With a few exceptions, line breaks, spaces and tabs are all the same for XML, and it does not matter if you use one, two, ten or a million of them. The only sane way to work with XML is to use XML tools. XML::LibXML is one of those tools.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: placing an existing line from my XML file to the top of the file
by james28909 (Deacon) on Aug 19, 2015 at 01:30 UTC
    Are you wanting to add the last line to the top and get rid of it at the bottom? Or just add last line to the top and keep rest of the file as is? I suggest you make a backup on you own before you try using this (if you do use it)

    This will leave the last line in the file while putting it at the beginning as well:

    use strict; use warnings; my @array; open( my $orig, '<', 'original_file' ); open( my $backup, '+>', 'original_backup' ); while (<$orig>) { push ( @array, $_ ); print( $backup $_ ); } close($orig); open( my $new_orig, '+>', 'original_file' ); print( $new_orig $array[-1] ); print( $new_orig @array ); close( $new_orig );

    This will copy the last line to the beginning while removing it from the end

    use strict; use warnings; my @array; open( my $orig, '<', 'original_file' ); open( my $backup, '+>', 'original_backup' ); while (<$orig>) { push( @array, $_ ); print( $backup $_ ); } close($orig); open ( my $new_orig, '+>', 'original_file' ); print( $new_orig pop @array ); print( $new_orig @array ); close ( $new_orig );

    Sample input data:

    is a test!!! this

      Thank you James! the code for "copying the last line to the beginning while removing it from the end" was exactly what I needed my script to do.

        I wouldnt use that for very large file btw.
Re: placing an existing line from my XML file to the top of the file
by poj (Abbot) on Aug 19, 2015 at 19:18 UTC

    I'm assuming that's only part of the file and you may have multiple testsuite elements. I don't understand the example though as I would have expected the testsuite time and counts to be the sum the testcases.

    #!perl use strict; use XML::Twig; my $t = XML::Twig->new( twig_handlers => { 'testcase' => \&testcase, 'testsuite' => \&testsuite, }, pretty_print => 'indented',); $t->parsefile( 'test.xml' ); $t->print_to_file('new.xml'); $t->print(); my @case=(); sub testcase { my ($t,$e) = @_; push @case,$e->cut; }; sub testsuite { my ($t,$e) = @_; while ($_ = pop @case){ $_->paste($e); } };
    poj

      Thanks Poj, that is correct. This is only one part of the Testsuite. Our system will contain multiple testsuites for each group of testcases. for instance, in the example I posted, it's the "combination" Testsuite which contain 20+ testcases. I've truncated the spreadsheet so that it only ran 5 testcases for the purpose of this thread. Every XML file will need to have this format in order for our test Metrics to be uploaded.