Thanks ... Thanks, Paryushan
-----Original Message----- From: Rob Dixon [mailto:rob.di...@gmx.com] Sent: Friday, February 06, 2009 9:23 PM To: Perl Beginners Cc: Sarsamkar, Paryushan Subject: Re: Reading file and changing contents which are not in one line Sarsamkar, Paryushan wrote: > Hi All, > > > > I have one xconf file whose contents are as follows. I actually want to > search for a property and change its value. Now what happens in my code > is while reading the file, each line is stored in $_ so actually the > line in my file is slit up in 2 or more lines, so I can not find the > property and look for value in the same line and change it. How can I do > that? > > > > File contents - > > ------------------------------------------------------------------------ > -- > > <?xml version="1.0" encoding="UTF-8"?> > > <!DOCTYPE Configuration > > SYSTEM "xconf.dtd"> > > <Configuration xmlns:xlink="http://www.w3.org/1999/xlink"> > > <Property name="wt.java.cmd" overridable="true" > targetFile="codebase/wt.properties" > > value="D:\ThirdParty\Java1.6\jre\bin\java.exe"/> > > <Property name="installedProduct.location.Apache" overridable="true" > > targetFile="codebase/WEB-INF/ieWebServerLocation.txt" > > value="D:\views\psarsamk_view\Apache"/> > > </Configuration> > > ------------------------------------------------------------------------ > -- > > > > What I want to do is find one property and change its value. Example - > find "wt.java.cmd" and change is value from > "D:\ThirdParty\Java1.6\jre\bin\java.exe" to "D:\ABC\java.exe" It is far better to parse XML data properly and use one of the XML Perl modules to manipulate it than to try to treat it as an ordinary text file. The code below uses XML::DOM to do what you describe. I hope it helps. Rob use strict; use warnings; use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parse(\*DATA); foreach my $child($doc->getDocumentElement->getChildNodes) { if ($child->getNodeName eq 'Property' and $child->getAttribute('name') eq 'wt.java.cmd') { $child->setAttribute(value => 'D:\ABC\java.exe'); } } $doc->printToFileHandle (\*STDOUT); __DATA__ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configuration SYSTEM "xconf.dtd"> <Configuration xmlns:xlink="http://www.w3.org/1999/xlink"> <Property name="wt.java.cmd" overridable="true" targetFile="codebase/wt.properties" value="D:\ThirdParty\Java1.6\jre\bin\java.exe"/> <Property name="installedProduct.location.Apache" overridable="true" targetFile="codebase/WEB-INF/ieWebServerLocation.txt" value="D:\views\psarsamk_view\Apache"/> </Configuration> -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/