LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2011, 08:15 AM   #1
talla
LQ Newbie
 
Registered: Jan 2010
Location: Berlin
Posts: 16

Rep: Reputation: 1
Perl: Simple Replacement of values in XML


Hey guys,

sorry to bother you, but I'm not a big perl man and badly need this. Thanks for any help in advance!

I Have an XML file like:

Code:
<configuration>
...
        <property>
                <key>mykey1</key>
                <value>value1</value>
        </property>
...
</configuration>
and I need a perl script that enables me to change values for certain keys. ideally something like ./changevalue.pl filename.xml mykey1 NEWVALUE

If there is any perl geek around: Thank you!

Cheers
Philipp
 
Old 05-11-2011, 08:48 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by talla View Post
Hey guys,

sorry to bother you, but I'm not a big perl man and badly need this. Thanks for any help in advance!

I Have an XML file like:

Code:
<configuration>
...
        <property>
                <key>mykey1</key>
                <value>value1</value>
        </property>
...
</configuration>
and I need a perl script that enables me to change values for certain keys. ideally something like ./changevalue.pl filename.xml mykey1 NEWVALUE

If there is any perl geek around: Thank you!

Cheers
Philipp
Start from reading http://search.cpan.org/search?query=XML+parser&mode=all . But it looks you'll have to learn Perl first.
 
Old 05-11-2011, 09:20 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,804

Rep: Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069
Does it have to be perl? I could probably do it with awk or sed.
 
Old 05-11-2011, 09:36 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Is there anything 'XML-specific' about this? Simple substitution, using sed seems to be the easiest way to meet your spec.
Code:
sed s/mykey1/NEWVALUE/g filename.xml
If it has to be Perl, and is truly an XML parsing exercise, then you need to look at using a full-on XML parser package, such as XML::Simple, XML::Parser, or several others available on CPAN.

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
XML::Simple usage in perl bharatbsharma Programming 2 05-07-2010 06:57 AM
problem while installing perl xml simple on fedora 9.... Snehal.lamture Linux - Newbie 6 02-19-2009 12:25 AM
perl: simple one line replacement, simple error message! pwd_pwd_omg_pwd Programming 8 11-19-2008 01:53 PM
LXer: XML::Simple for Perl developers LXer Syndicated Linux News 0 02-04-2007 09:21 AM
Perl XML::Simple - Hash Question smaida Programming 2 05-26-2004 05:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration