Link to home
Start Free TrialLog in
Avatar of gurudasha
gurudasha

asked on

To delete and replace and xml element using XML::Twig

I have a requirement to replace and xml element

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<JavaHost>
.............
..........
<ABC>
      <InputStreamLimitInKB>5625</InputStreamLimitInKB>
      <ReadRequestBeforeProcessing>true</ReadRequestBeforeProcessing>
   </ABC>
....
...
</javahost>

If InputStreamLimitInKB is 5625 i have to change it to 3268
The xml file should get updated.

Please help as i am new to xml twig
Avatar of Suhas .
Suhas .
Flag of United States of America image

on command prompt run the following perl one liner.

perl -pi -e 's/5625/3268/g' file.xml
The problem with suhasbharadwaj's answer is that it is non-specific.  If 5625 occurs anywhere in the file (even as part of another number), it will replace it.  I would recommend this instead:
perl -pi -e 's{<InputStreamLimitInKB>5625</InputStreamLimitInKB>}{<InputStreamLimitInKB>3268</InputStreamLimitInKB>}gi' file.xml

Open in new window

If the XML is structured such that the tags will ever not be on the same line as the value for InputStreamLimitInKB, then the above will need to be tweaked.

You can also do the above as part of another script using a loop.  If you want to replace it in-place in the file, I'd suggest Tie::File if you want it as part of another script.
foreach (<INPUT>) {
    s{<InputStreamLimitInKB>5625</InputStreamLimitInKB>}{<InputStreamLimitInKB>3268</InputStreamLimitInKB>}gi;
}

Open in new window


I can not give you an XML::Twig answer as I use other XML modules.
ASKER CERTIFIED SOLUTION
Avatar of gurudasha
gurudasha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gurudasha
gurudasha

ASKER

Because i used the module i was asking for