Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl script to change value in an xml file

Posted on 2013-06-21
13
Medium Priority
?
595 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

886 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