HEllo,
I've found a script (at this url [url]http://www.thescripts. com/forum/thread84554.htm l [/ur]) that is like the things i want to do;
i want to parse my xml file and modify the value of an attribute;
for example modify this
in that
So, i've modify the script
my xml file
i obtains this errors , i don't understand ??
normaly the value of the attribute type must be changed.
thanks
I've found a script (at this url [url]http://www.thescripts. com/forum/thread84554.htm l [/ur]) that is like the things i want to do;
i want to parse my xml file and modify the value of an attribute;
for example modify this
<nom name="pivot">
<information valeur="Niveau" type="Bon"/>
</nom>
<information valeur="Niveau" type="Bon"/>
</nom>
<nom name="pivot">
<information valeur="Niveau" type="Mauvais"/>
</nom>
<information valeur="Niveau" type="Mauvais"/>
</nom>
So, i've modify the script
Code:
#!/usr/bin/perl -w
use strict;
use XML::XPath;
use XML::XPath::XMLParser;
use XML::Twig;
# create an object to parse the file and field XPath queries
# my $xpath = XML::XPath->new( filename => shift @ARGV );
my $xpath = XML::XPath->new( filename => "client.xml" );
# apply the path from the command line and get back a list matches
my $field;
my @field = 'string';
my $old_value = $xpath->find("//nom[\@name='pivot']/information/\@type" );
#find("//nom[\@name='pivot']/information[\@type]/text()" );
print $old_value."\n";
#qq{$field\[string() = "$old_value"]}
my $new_value = 'Tres BOB';
my $t = new XML::Twig( TwigRoots =>
qq{$field\[string() = "$old_value"] => \&update} ,
TwigPrintOutsideRoots => 1,);
$t->parsefile( 'client2.xml' );
$t->flush;
sub update
{
my( $t, $field_elt)= @_;
$field_elt->set_text( $new_value);
$field_elt->print;
}
my xml file
<?xml version="1.0" encoding="windo ws-1250"?>
<root value="x">
<entreprise>som e text</entreprise>
<info></info>
<client>
<nom name="pivot">
<information valeur="Niveau" type="Bon"/>
</nom>
<nom name="paul">
<information valeur="Niveau" type="Bon">xxx</information>
<information valeur="Solvabl e" type="Mauvais"> zoooooooo</information>
</nom>
</client>
<client>
<nom name="albine">
<information valeur="Solvabl e" type="Bon">azer </information>
</nom>
</client>
<client>
<nom name="Terence">
<information valeur="Niveau" type="Tres bon"/>
<information valeur="Solvabl e" type="Bon"/>
<information valeur="Ancien" type="Oui"/>
</nom>
</client>
</root>
<root value="x">
<entreprise>som e text</entreprise>
<info></info>
<client>
<nom name="pivot">
<information valeur="Niveau" type="Bon"/>
</nom>
<nom name="paul">
<information valeur="Niveau" type="Bon">xxx</information>
<information valeur="Solvabl e" type="Mauvais"> zoooooooo</information>
</nom>
</client>
<client>
<nom name="albine">
<information valeur="Solvabl e" type="Bon">azer </information>
</nom>
</client>
<client>
<nom name="Terence">
<information valeur="Niveau" type="Tres bon"/>
<information valeur="Solvabl e" type="Bon"/>
<information valeur="Ancien" type="Oui"/>
</nom>
</client>
</root>
i obtains this errors , i don't understand ??
normaly the value of the attribute type must be changed.
Bon
Use of uninitialized value in concatenation (.) or string at C:\Documents and Se
ttings\donny\Bu reau\bigs\parsr .pl line 25.
Can't use string ("[string() = "Bon"] => &update") as a HASH ref while "strict r
efs" in use at C:/Perl/site/lib/XML/Twig.pm line 1303.
Use of uninitialized value in concatenation (.) or string at C:\Documents and Se
ttings\donny\Bu reau\bigs\parsr .pl line 25.
Can't use string ("[string() = "Bon"] => &update") as a HASH ref while "strict r
efs" in use at C:/Perl/site/lib/XML/Twig.pm line 1303.
thanks
Comment