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
gurudashaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Suhas .QA ManagerCommented:
on command prompt run the following perl one liner.

perl -pi -e 's/5625/3268/g' file.xml
0
wilcoxonCommented:
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.
0
gurudashaAuthor Commented:
Thank you.
The answers are correct, but wanted using XML::Twig.
I was able to find it out.

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gurudashaAuthor Commented:
Because i used the module i was asking for
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.