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

Hi,
Good Evening. I am using the xpath in my program and i an retrive and update the values. But I am unable to save the file. I have attached the code bellow.

use XML::XPath; $name = "Vinodh"; my $file = '../main/memlist.xml'; my $xp = XML::XPath->new(filename=>$file); my $nodeset = $xp->find("/MEMBERLIST/LIST[NICK='$name']/ROWID"); $data = $xp->getNodeText("/MEMBERLIST/LIST[NICK='$name']/NICKID"); print $data; $val = "9999"; $xp->setNodeText("/MEMBERLIST/LIST[NICK='$name']/NICKID",$val);

My Xml file is

<?xml version="1.0" encoding="Windows-1252"?> <MEMBERLIST> <LIST><NICK>0</NICK><ROWID>100</ROWID><NICKID>12545</NICKID><RANK>9</R +ANK></LIST> <LIST><NICK>007</NICK><ROWID>101</ROWID><NICKID>10125</NICKID><RANK>5< +/RANK></LIST> <LIST><NICK>Vinodh</NICK><ROWID>102</ROWID><NICKID>12498</NICKID><RANK +>18</RANK></LIST> <LIST><NICK>0P</NICK><ROWID>103</ROWID><NICKID>5472</NICKID><RANK>4</R +ANK></LIST> </MEMBERLIST>

U tell how to save the document in the file
Thanks & Advance Wishes

20060814 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: How to save a XML File using XPath
by derby (Abbot) on Aug 14, 2006 at 13:18 UTC

    my $str = $xp->getNodeAsXML(); print $str;

    -derby
Re: How to save a XML File using XPath
by gellyfish (Monsignor) on Aug 14, 2006 at 13:13 UTC

    XML::XPath doesn't really have have a mechanism for getting the whole document as a string so you have to do something like:

    my $document = ($foo->findnodes('/'))[0]->toString();
    and then you can print that to a file in the usual way.

    Update: derby, is of course correct. The (unfortunately undocumented) getNodesAsXML() does  return $self->findnodes_as_string('/'); if no path is provided, however findnodes_as_string() comes with the caveat: "The result is not guaranteed to be valid XML though.", which may or may not also be true of my version.

    /J\

Re: How to save a XML File using XPath
by Tanktalus (Canon) on Aug 14, 2006 at 15:23 UTC

    To be honest, this looks like pretty much exactly what XML::Twig is written for. And, unlike findnodes_as_string, I'm pretty sure that mirod guarantees valid XML output ;-)

    I may be biased, though, as my decision tree on XML is a bit sparse.