LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: Simple Replacement of values in XML (https://www.linuxquestions.org/questions/programming-9/perl-simple-replacement-of-values-in-xml-880064/)

talla 05-11-2011 07:15 AM

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

Sergei Steshenko 05-11-2011 07:48 AM

Quote:

Originally Posted by talla (Post 4353156)
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.

grail 05-11-2011 08:20 AM

Does it have to be perl? I could probably do it with awk or sed.

theNbomr 05-11-2011 08:36 AM

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.


All times are GMT -5. The time now is 01:57 PM.