Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Is there any possible way to convert XML::Twig object to XML::XPath object

by rnd (Initiate)
on Oct 19, 2011 at 06:51 UTC ( #932306=perlquestion: print w/replies, xml ) Need Help??
rnd has asked for the wisdom of the Perl Monks concerning the following question:

I'm using XML::XPath for a long time to parse xml, now the situation has come to modify the attribute value.

xml is available in a file, it will be very similar to this.

<AppName>

<Action AllDay='1'StartRace='1'/>

<StartPoint AM_PM='AM' Hours='09' Mins='30'/>

<EndPoint AM_PM='PM' Hours='06' Mins='30'/>

</AppName>

I have written a perl script to change the Hours of StartPoint tag,
use strict; use warnings; use XML::Twig; my $fileName = 'Conf.xml' my $twig = XML::Twig->new( twig_roots => { StartPoint => \&ReplaceAtriValue }, twig_print_outside_roots => 1, ); sub ReplaceAtriValue { my ($twig, $startPoint) = @_; if ( '09' eq $startPoint->att('Hours') ) { $startPoint->set_att( Hours => '12'); } $startPoint->print(); } $twig->parsefile($fileName);
Now $twig has the xml object. If I send this object($twig) to my SetWindow.pm which parse xml using XML::XPath, did not recognize $twig object.

Is there any possible way to convert XML::Twig object to XML::XPath object so that XML::XPath will parse values in the $twig object.

Actually I have using XML::XPath for long back hence I have to send the XML::XPath object to all my own *.pm like SetWindow.pm, at the same time i need to change the values of the attribute of the standard xml before sending to my own PMs.

Thanks in Advance

Any Help is much appreciated

  • Comment on Is there any possible way to convert XML::Twig object to XML::XPath object
  • Download Code

Replies are listed 'Best First'.
Re: Is there any possible way to convert XML::Twig object to XML::XPath object
by ikegami (Pope) on Oct 19, 2011 at 07:34 UTC
    You want the resulting XML, not some twig. Instead of printing the resulting XML, place it in a variable.

      Thanks for your reply ikegami.

      I'm not sure how to assign a output of modified xml into a variable.

      Actually 'twig_print_outside_roots => 1,' prints the output in command prompt. I don't know how to redirect the root output to a variable.

        You can open a filehandle on a variable: my $s; open my $fh, ">", \$s; print {$fh} "this goes in the string";, and send the output of twig_print_outside_roots to it by writing twig_print_outside_roots => $fh.

        It is still a bad idea though. Why use 2 different XML modules when one could work?

Re: Is there any possible way to convert XML::Twig object to XML::XPath object
by Anonymous Monk on Oct 19, 2011 at 07:00 UTC
    No.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932306]
Approved by ikegami
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2016-12-04 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (63 votes). Check out past polls.