Saving a file in Perl XPath
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...
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
[730 byte] By [
pr0fess0r] at [2007-11-19 2:50:20]

# 1 Re: Saving a file in Perl XPath
To the best of my knowledge XML::XPath doesn't have any real save support. I suppose you might try something like
XML::XPath::XMLParser::as_string($xp->find('/'))
To write the XML document to a string, and then write the write the returned string to a file.
But all in all I think you would be better off switching to the XML::GDOME module which has more official save support through the XML::GDOME::Document::toString(mode) function.
http://search.cpan.org/~tjmather/XML-GDOME-0.86/lib/XML/GDOME.pod
khp at 2007-11-10 3:27:53 >

# 2 Re: Saving a file in Perl XPath
Thanks for the tip - I havent got time to go and learn another module :) You know how it is working with clients.
Whats the best way to dump this to a file?
my $nodeset=$xp->find('/');
foreach my $node ($nodeset->get_nodelist) {
# What do I do here to get each node as a string?
$nodeString=??;
print FILEHANDLE $nodeString."\n";
}
Thanks for helping a poor n00b
cheers
Lucas
# 3 Re: Saving a file in Perl XPath
Whats the best way to dump this to a file?
I really can't answer that any better than I already have.
khp at 2007-11-10 3:29:52 >
