Solved

perl script to change value in an xml file

Posted on 2013-06-21
13
564 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 110

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 110

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql update statement 3 33
Help needed with Powershell  XML to MySQL 5 71
MySQL Memory Keeps Increasing 4 62
Combining Queries 7 38
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

679 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