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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open and write in a file 1

Status
Not open for further replies.

donny750

Programmer
Jul 13, 2006
145
FR
hello,

I've a script that modify a value of an attribute in an xml file ,
the script modify the attribut but the problem is that the result is print in the shell

my script

Code:
#!/usr/bin/perl
use XML::Twig;

my $filename = './it2.xml';
open(FILE, '>', $filename) || die("impossible open $filename ");

my $t= XML::Twig->new( 
           twig_roots   => { 'top' => \&its, },    
           twig_print_outside_roots => 1,               
                      );
  $t->parsefile( 'its.xml');
 
  sub its 
    { my( $t, $price)= @_;
      my $currency=  $price->{'att'}->{'bat'};          
      if( $currency eq 'Except')
        { $usd_price= $price->text;   
                     
   
          my $euro_price= $usd_price ;
          $price->set_text( $euro_price);               
          $price->set_att( VALUE => 'FINE');          
        }
     
      $price->print;                                    
    }

and i can't verify if the modification are good because the output is in the shell and i've a big xml file

i've tried this
$price-> FILE print;it's not run
and this
print FILE $price;it's change nothing

thanks
 
Try this...
Code:
my $xml_data = $price->print;
print FILE $xml_data;



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
i've tried
Code:
      $price->print;   
       $globo=$price;   
      print FILE $globo;                              
    }
but in the file i've this
XML::Twig::Elt=HASH(0x1e2a688)XML::Twig::Elt=HASH(0x1afbf84)XML::Twig::Elt=HASH(0x1afbff0)

 
no i think $price is an object and ->print is the method, have you tried...
Code:
print FILE $price->print;
also don't forget to add
Code:
close(FILE);
afterwards, which I can't see in your example

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
i've tried and the file is empty
but i know the solution is with this
twig_print_outside_roots => 1,
 
i think you need one of the PERL experts to help, this OO thing is a bit beyond me at present :-(

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
maybe:

Code:
open(FILE, '>', $filename) || die("impossible open $filename ");

[rest of code here]

$price->print( \*FILE);

thats a big module with tons of functions/methods. Try reading through the documentation if the above does not work.


- Kevin, perl coder unexceptional!
 
I try reading thanks

with
$price->print( \*FILE);
it's run but i've not the total outup i've in the console(the shell window)

when i run the script in the console i've this
<POST>
<INITIAL>
<FINDER>
<FIRST VERSIONNUMBER ="1">

<even BUSINESSNAME ="" DATATYPE ="string" DESCRIPTION ="No" />
<top bat ="'Except'" VALUE ="FINE">
</top>
<top bit ="Thousand" VALUE ="None">
</top>
<top boot ="Decimal" VALUE ="TH">
</top>
</FIRST>
</FINDER>
</INITIAL>
</POST>

in the file in output i've this
<top bat ="'Except'" VALUE ="FINE">
</top>
<top bit ="Thousand" VALUE ="None">
</top>
<top boot ="Decimal" VALUE ="TH">
</top>

my xml file is like this
<POST>
<INITIAL>
<FINDER>
<FIRST VERSIONNUMBER ="1">

<even BUSINESSNAME ="" DATATYPE ="string" DESCRIPTION ="No" />
<top bat ="'Except'" VALUE ="Yes"/>
<top bit ="Thousand" VALUE ="None"/>
<top boot ="Decimal" VALUE ="TH"/>
</FIRST>
</FINDER>
</INITIAL>
</POST>
 
i've found

i've modify this and the output is in my file and nothing in the console
Code:
 twig_print_outside_roots => \*FILE,               
                      );
 
i've problem
i'm looking my output file and the script add tag

result in my output xml file
<?xml version="1.0" encoding="windows-1250"?>

<POST>

<INITIAL>

<FINDER>

<FIRST VERSIONNUMBER ="1">



<even BUSINESSNAME ="" DATATYPE ="string" DESCRIPTION ="No" />

<top VALUE="None" bat="Except"></top><POST><top VALUE="None" bat="Except"></top>

<top VALUE="None" bit="Thousand"></top><top VALUE="None" bit="Thousand"></top>

<top VALUE="TH" boot="Decimal"></top><top VALUE="TH" boot="Decimal"></top>

</FIRST>

</FINDER>

</INITIAL>

</POST>

my script

Code:
#!/usr/bin/perl
use XML::Twig;








my $filename = './exit.xml';
#Ouverture du fichier en ecriture
open(FILE, '>', $filename) || die("impossible to open $filename ");



    
    my $t= XML::Twig->new( 
           twig_roots   => { 'top' => \&its, },    
           twig_print_outside_roots => \*FILE,               
                      );
  $t->parsefile( 'first.xml');
  $t->flush;
 
 
  sub its
    { my( $t, $name)= @_;
      my $nom=  $name->{'att'}->{'bat'};          
      if( $nom eq 'Except')
        { $old= $name->text;   
                  
          my $newn= $old;
          
          $name->set_text( $newn); 
                      
          $name->set_att( VALUE => 'None');  
                
        }
     
     $name->print(\*FILE);
     $name->flush;   
                                    
    }

close(FILE);

and the xml file that i want to parse
<?xml version="1.0" encoding="windows-1250"?>
<POST>
<INITIAL>
<FINDER>
<FIRST VERSIONNUMBER ="1">

<even BUSINESSNAME ="" DATATYPE ="string" DESCRIPTION ="No" />
<top bat ="Except" VALUE ="Yes"/>
<top bit ="Thousand" VALUE ="None"/>
<top boot ="Decimal" VALUE ="TH"/>
</FIRST>
</FINDER>
</INITIAL>
</POST>

in the output xml file he add this in the 15 line
<POST><top VALUE="None" bat="Except"></top>


and in the console i've this

also my xml output file are not valid because he have added tags and i don't know why

??
 
if my xml file is like this

<?xml version="1.0" encoding="windows-1250"?>
<POST>
<INITIAL>
<FINDER>
<FIRST VERSIONNUMBER ="1">

<even BUSINESSNAME ="" DATATYPE ="string" DESCRIPTION ="No" />
<top bat ="Except" VALUE ="Yes"/>
<top bit ="Thousand" VALUE ="None"/>
<top boot ="Decimal" VALUE ="TH"/>
<tap boot ="Exit" VALUE ="Yes"/>
<tap boot ="Decimal" VALUE ="TH"/>
</FIRST>
</FINDER>
</INITIAL>
</POST>

and i want to modify the tag top and the tag tap
must have make 2 functions ?
and how can i call them ?

thanks
 
i'm glad all this makes sense to you :) - have a star!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top