Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

perl script to change value in an xml file

Posted on 2013-06-21
13
Medium Priority
?
584 Views
Last Modified: 2013-08-04
Hello All,

I have an xml file for example it is as below

<context-param>
      <param-name>abcde.dffg.</param-name>
      <param-value>off</param-value>
   </context-param>
   <context-param>
      <param-name>scbjbk.dfdff.sdfdf</param-name>
      <param-value>false</param-value>
   </context-param>
   <context-param>
      <param-name>sdadsd.LOGGER_LEVEL</param-name>
      <param-value>OFF</param-value>
   </context-param>
   <context-param>
      <param-name>ssdds.DEBUG_JAVASCRIPT</param-name>
      <param-value>FALSE</param-value>
   </context-param>
   <context-param>
      <param-name>hello.hoareyou.MYCONTENTVALUE</param-name>
      <param-value>true</param-value>
   </context-param>

My script should look into the xml file and search for "MYCONTENTVALUE" and then change the <param-value>false<param-value>

After the script runs the file should be changed to like below

<context-param>
      <param-name>abcde.dffg.</param-name>
      <param-value>off</param-value>
   </context-param>
   <context-param>
      <param-name>scbjbk.dfdff.sdfdf</param-name>
      <param-value>false</param-value>
   </context-param>
   <context-param>
      <param-name>sdadsd.LOGGER_LEVEL</param-name>
      <param-value>OFF</param-value>
   </context-param>
   <context-param>
      <param-name>ssdds.DEBUG_JAVASCRIPT</param-name>
      <param-value>FALSE</param-value>
   </context-param>
   <context-param>
      <param-name>hello.hoareyou.MYCONTENTVALUE</param-name>
      <param-value>
false
</param-value>
   </context-param>

It should not use any module and this should be done by plain regular expression.
Can someone help please
0
Comment
Question by:gurudasha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39265754
It should not use any module and this should be done by plain regular expression.
That tells me that this is your class homework assignment.

What have you tried?

What errors/warnings are you receiving?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39265911
If you're open to using a PHP solution, I will be glad to show you how this is done.  Please post back and let us know.
0
 
LVL 27

Assisted Solution

by:wilcoxon
wilcoxon earned 750 total points
ID: 39266881
Here's a one-liner that will do what you want...
perl -i.bak -ne "local $/ = undef; $str = $_ . <>; $str =~ s{(?<=MYCONTENTVALUE</param-name>)(\s*<param-value>)true(</param-value>)}{${1}false$2}; print $str" file.xml

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
tel2 earned 375 total points
ID: 39267860
Hi Experts,
I suggest we wait for gurudasha's response to FishMonger's comment before handing out solutions.

Hi gurudasha,
Could you please answer FishMonger's questions and also tell us why you are not wanting to use any modules.

Thanks.
tel2
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 39267902
The PHP solution would envision using SimpleXML to create an object, modify its properties and render it as XML.
http://php.net/manual/en/book.simplexml.php
0
 

Author Comment

by:gurudasha
ID: 39268432
Thank you guys for your solution.

Is there a way to use a DOM/saax parser for this.
0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 39268847
Sure but that's not what you asked.  Your question specifically said without using any modules and using regexes.  Why the change?

@FishMonger/tel2, just because he specified no modules does not mean it is a homework assignment.  I've seen many work environments where they have very limited (and/or very out-of-date) modules which can make "no modules" simpler to ask than for one that will work only with core modules from some old version of perl (with the differences that existed in those versions).
0
 
LVL 12

Expert Comment

by:tel2
ID: 39268925
Hi wilcoxon,
> just because he specified no modules does not mean it is a homework assignment.
I know, but it's an indicator, and considering the EE guidelines, I don't think experts should ignore such indicators.  To reduce any doubt I asked:
> and also tell us why you are not wanting to use any modules
Another indicator is the lack of response/explanation to such questions, which is why I have resisted improving on your solution.  Let's see whether he answers your question.

tel2
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 39269419
I ditto the response from tel2.
0
 

Author Comment

by:gurudasha
ID: 39269655
The servers are in restricted environment, so usage of modules other than standard modules is tough. If there is only 1 or 2 pm files i can add with the code, that should be fine.

Since it's not a very good idea to use regular expressions for parsing xml, as xml file can change sometime, i thought it would be a good idea to use any dom/saax parser incase not many module pm files need to be added. That would be more reliable.
0
 
LVL 27

Assisted Solution

by:wilcoxon
wilcoxon earned 750 total points
ID: 39270003
The only XML parser I know of that does not rely on a C library is XML::SAX::PurePerl which still depends on File::Temp (core), XML::NamespaceSupport, and XML::SAX::Base.

So you'll need to install at least 3 distributions (or at least those modules from the distributions) to use XML::SAX::PurePerl (and this is about as good as you'll get).

Also, note that XML::SAX::PurePerl notes that it is *extremely* slow and recommends using something else.
0
 

Assisted Solution

by:gurudasha
gurudasha earned 0 total points
ID: 39367362
Thank you guys.
I used XML::Twig and it seemed to work fine
0
 

Author Closing Comment

by:gurudasha
ID: 39380397
Also selected my answer because i used that as a solution.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Six Sigma Control Plans
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question