Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How can I replace a line (tag) in an XML file?

by AlexTape (Monk)
on Sep 29, 2011 at 10:00 UTC ( #928511=note: print w/ replies, xml ) Need Help??


in reply to How can I replace a line (tag) in an XML file?

is the view of the xml fix or is it only your thinkin of storing the data on this way?

use strict; use XML::Simple; $filename = "yours.xml"; xml_edit($filename); # return done 1 || error 0 sub xml_edit($){ my$bool=0; %DB=load($_[0]); if(exists$DB{student}{id}{2}) { $DB{student}{gpa}='C'; $bool++; } save($_[0],\%DB); return$bool; } #untested
but i recommend you to build a hash of your data within perl and export it via "save($filename,\%hashref);"

for this you got truly the right structure with easy editing potential even if the data gets more bulky.
$perlig =~ s/pec/cep/g if 'errors expected';


Comment on Re: How can I replace a line (tag) in an XML file?
Download Code
Re^2: How can I replace a line (tag) in an XML file?
by Jenda (Abbot) on Sep 29, 2011 at 10:19 UTC

    The problem with XML::Simple (well, one of) when using it to edit a file is that it tends to change the XML in ways that do not make a difference when the file is read by XML::Simple, but will cause all other tools to fail to read it.

    In this particular case all those content-only tags will become attributes of the parent tag. XML::Simple will not mind, other tools most probably will.

    Not speaking about the fact that your code would not work ... there is no load() or save() in XML::Simple and the data structure would look different than what you seem to think.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      im sorry - sunken in my own projects i should probably post full storys ;-)
      sub load{return%{XML::Simple->new(ForceContent=>1)->XMLin($_[0])};}#lo +ads xml sub save{XMLout($_[1],OutputFile=>$_[0]);1;}#saves hash to xml
      but your still mostly right.. for me it works fine..
      $perlig =~ s/pec/cep/g if 'errors expected';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://928511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-09-30 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (372 votes), past polls