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
Solved

perl script to change value in an xml file

Posted on 2013-06-21
13
557 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 109

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 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 12

Accepted Solution

by:
tel2 earned 125 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 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 26

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 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Grouping 2 48
updating the date data 12 34
two ways encryption with php 3 37
Uploading a CSV Data Import via PHP & MySql 3 32
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

861 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