Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving an XML file modified by XPath

Status
Not open for further replies.

pr0fess0r

Programmer
Jul 10, 2001
25
NZ
Hi
Is it possible to save a modifed XML file in Perl's XPath?
I have a script that looks for blank id attributes in an xml file and populates them with a unique ID...

Code:
my $updated=0;
my $xp = XML::XPath->new(filename => $file);
my $nodeset = $xp->find('//@id'); # find all ids
     foreach my $node ($nodeset->get_nodelist) {
               my $id=$node->string_value;
               if ($id eq ""){
                    $xp->setNodeText('@id',getUniqueId());
                                $updated=1;
               }
     }
if($updated){
# How do I save the XML file here?
}

Am I on the right track? How do I save the XML?
many thanks in advance!

Lucas
 
Lucas,
does it work,

or doesn't it

yo, give us something to work with, I'm not running your code b'c I have no idea what you're trying to do


Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Sorry if I wasnt clear
The code above successfully appears to add a unique integer to any blank id attributes it finds, what I need to be able to do is update $file with the change. From what I read of XPath, there is a way to dump all of the nodes out as one string and write them to a file, but each node will have a namespace element as well, and I'd then need to strip that. So I'm wondering if there's a better, simpler way to just dump the XPath object back out to the xml $file again... basically I need the magic line of code to replace
# How do I save the XML file here?

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top