Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to replace the existing values and to add the new nodes which are not existed based on the properties file

by perladdict (Chaplain)
on Apr 01, 2014 at 15:19 UTC ( #1080587=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Monks,
I am very new to the XML data manipulation using Perl, some process will generate the iprocess XML, in which few values are not populated correctly, Due to this client are editing this xml manually by using the properties file. I need to automate this task by using the Perl. I don't have any prior experience for this task.Below is the detail description of task which i need to complete.
The input for editing the XML is properties file, in which the lines starts with M_R signifies replacement of the values for those attributes which are existed in XML and M_A are the lines for which i need to add new nodes in the XML. Below it the properties file content and the XML which i need to modify based on the properties file.

#Below is the Properties file content M_R_xxxx/nodes/Application = test123 M_R_params/nodes/Environment = test M_R_Params/Debug/log = /admin/test/xxxx/yyyy/at/domain/test123-uat/app +lication/logs/a.log M_R_/nodes/srv/new/build/binding/machine = tesing.com M_A_/app/ser/ada/bind/bin/host = google.com M_R_/ap/ser/mn/bin/bin/product/ver = 1.2.3 M_R_/app/serv/SN/bind/bin/set/perl/size = 566 M_A_/appl/ser/CN/bind/bin/set/perl/Path = /xxxx/APP/app1/alter/value M_A_/appn/ser/XT/bind/bin/set/perl/Path = /new/test/app/minor/test123 M_R_/applx/serx/NT/bind/binding/down/time = 30 M_R_/appl/serv/PT/context/test/no = 25 M_R_/appl/ser/XT/gate/clear/state = false

The XML file which i need to modify based on the above properties file is as below.
<?xml version="1.0" encoding="UTF-8"?> <list> <entry> <name><first>Thadeus</first><last>Wrigley</last></name> <phone>716-505-9910</phone> <address> <street>105 Marsupial Court</street> <city>Fairport</city><state>NY</state><zip>14450</zip> </address> </entry> <entry> <name><first>Jill</first><last>Baxter</last></name> <address> <street>818 S. Rengstorff Avenue</street> <zip>94040</zip> <city>Mountainview</city><state>CA</state> </address> <phone>217-302-5455</phone> </entry> <entry> <name><last>Riccardo</last> <first>Preston</first></name> <address> <street>707 Foobah Drive</street> <city>Mudhut</city><state>OR</state><zip>32777</zip> </address> <phone>111-222-333</phone></entry> <entry> <address> <street>10 Jiminy Lane</street> <city>Scrapheep</city><state>PA</state><zip>99001</zip> </address> <name><first>Benn</first><last>Salter</last></name> <phone>611-328-7578</phone> </entry> </list>
From long time i am going through the threads which are related to XML in Perl monks,based on posts in perl monks i have installed XML::Parser,XML::libXML, based on the examples in the thread i could able to parse the XML but not able to achieve the task what actually its needed to accomplish this task.
I have started to read the O'REILLY Perl and XML PDF book which i have downloaded from Net, but this may take time for me to gain some knowledge on this.Experts monks could you please consider this post and consider it's an humble and very kind request from me to give your valuable inputs for me to get start with this task.

Comment on How to replace the existing values and to add the new nodes which are not existed based on the properties file
Select or Download Code
Re: How to replace the existing values and to add the new nodes which are not existed based on the properties file
by choroba (Abbot) on Apr 01, 2014 at 21:25 UTC
    I usually use XML::XSH2, a wrapper around XML::LibXML, to process XML files. I tried the following:
    #!/usr/bin/perl use warnings; use strict; use XML::XSH2; package XML::XSH2::Map; our ($path, $value, $v); package main; xsh << 'end.'; open 1.xml ; register-namespace am http://www.abc.com/xml/appmanagement ; end. my ($replace, $add); open my $PROP, '<', 'properties' or die $!; while (<$PROP>) { chomp; s=^M_([RA])_/?== or next; my $action = $1; ($path, $value) = split / = /; xsh << ' end.'; $v = //am:name[.=$path]/../am:value ; if $v set $v/text() $value ; end. next if $v; $path =~ s=/=/am:=g; xsh "set /am:$path '$value'"; } xsh 'save :f new.xml';

    This was the created file:

    I am not sure that is what you exactly need, though. Especially in case of the last two lines of the properties file: the XPath matches 3 different nodes, it is not clear which one you want to modify.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hi Choroba, Thanks for your inputs, i tried to install the perl module in active Perl using PPM, but it is displaying no package found as below.

      C:\Perl\bin>ppm search XML::XSH2 *** no packages matching 'XML::XSH2' found ***
      I have already installed XML::LibXML,XML::DOM,XML::SAX moudles, but i unable to install XML::XSH2.Can you please suggest is XML::XSH2 supports on windows active perl?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1080587]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-21 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls