• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1058
  • Last Modified:

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"?>

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
  • 2
1 Solution
Suhas .QA ManagerCommented:
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>) {

Open in new window

I can not give you an XML::Twig answer as I use other XML modules.
gurudashaAuthor Commented:
Thank you.
The answers are correct, but wanted using XML::Twig.
I was able to find it out.

use XML::Twig;
my @abc = $root->children('ABC');
foreach (@abc)
			my $element = new XML::Twig::Elt('InputStreamLimitInKB', "$new_limit");
			$element->paste('first_child', $_); 

Open in new window

gurudashaAuthor Commented:
Because i used the module i was asking for
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now