Solved

perl script to change value in an xml file

Posted on 2013-06-21
13
528 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
Comment Utility
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
 
LVL 11

Accepted Solution

by:
tel2 earned 125 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
Comment Utility
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
Comment Utility
Thank you guys for your solution.

Is there a way to use a DOM/saax parser for this.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
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 11

Expert Comment

by:tel2
Comment Utility
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
Comment Utility
I ditto the response from tel2.
0
 

Author Comment

by:gurudasha
Comment Utility
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 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
Comment Utility
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
Comment Utility
Thank you guys.
I used XML::Twig and it seemed to work fine
0
 

Author Closing Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now