Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Swaping xml elements using XML::Twig

by aakikce (Acolyte)
on Apr 27, 2007 at 09:17 UTC ( [id://612339]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

i tried to swap xml elements. I am getting errors as "can't call method move without a package or object reference"

My xml file and code as follows:-

<root> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> </root> open (FIN, "< in.xml") || "error input xml"; $content = <FIN>; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse($content); $twig->get_xpath('//source')->move('before', $twig->get_xpath('//pages +ource')); $content = $twig->sprint; open (FOUT, ">output.xml") || "error"; print FOUT $content;

Thanks,

Replies are listed 'Best First'.
Re: Swaping xml elements using XML::Twig
by mirod (Canon) on Apr 27, 2007 at 09:43 UTC

    Well, while XML::Twig tries real hard to do what you want, it still doesn't understand that line:

    $twig->get_xpath('//source')->move('before', $twig->get_xpath('//pagesource'));

    You have to pay attention to what the methods return: here get_xpath returns a list of XML::Twig::Elt. When you try to apply an XML::Twig method to that (unblessed) array... you get the error message you reported. You have to loop over the elements of that array yourself, and move them one by one. Bummer, I know ;--)

    Here is my take on this:

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse(\*DATA); foreach my $elt ($twig->get_xpath('//source')) { $elt->move( before => $elt->prev_sibling( 'pagesource')); } $twig->print; # use print_to_file to print to a file __DATA__ <root> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> <pagesource> <para>Teacher's Guide Level A</para><para><graphic alt="title" links=" +Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph><paragraph>ISBN-10: 1 +-4190-4181-9</paragraph> </source> </root>
Re: Swaping xml elements using XML::Twig
by Samy_rio (Vicar) on Apr 27, 2007 at 09:43 UTC

    Hi aakikce, Try this,

    use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse(\*DATA); $_->move('before', $twig->get_xpath('//pagesource')) for ($twig->findn +odes('//source')); $twig->print; __DATA__ <root> <pagesource> <para>Teacher's Guide Level A</para> <para><graphic alt="title" links="Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph> <paragraph>ISBN-10: 1-4190-4181-9</paragraph> </source> <pagesource> <para>Teacher's Guide Level A</para> <para><graphic alt="title" links="Studio Logo R BW.tif"/></para> </pagesource> <source> <paragraph>ISBN-13: 978-1-4190-4181-5</paragraph> <paragraph>ISBN-10: 1-4190-4181-9</paragraph> </source> </root>

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://612339]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found