Re: XML - Find and replace text



O/H gcgaim@xxxxxxxxx έγραψε:
On Sep 19, 10:52 am, schl...@xxxxxxxxxxxxxxxx wrote:
gcg...@xxxxxxxxx wrote:
On Sep 19, 10:48 am, gcg...@xxxxxxxxx wrote:
What would be the cleanest way to parse an XML file and replace
certain pieces of text that match a specified pattern? I'm going to
know the exact "location" as in the text is always going to be at:
<a>
<b>
<c=stuff>text to replace here</c></b></a>
Any help is appreciated!
I'm using ActiveTcl if that makes any difference.
Cleanest is probably using XSLT with the tdom package.

Michael

OK, so assuming the XML structure is:

<install>
<service>
...
</service>
<service>
<url>
<data name="config1>this is what I want to change</data>
</url>
</service>
</install>

How do I set the value in <data name=config1>. This is what I have so
far.

package require tdom

set fp [open "config.xml" r]
set xml [read $fp]

set doc [dom parse $xml]
set root [$doc documentElement]

Thank you again!


Lets add some xpath search to the game :-)

set nodes [$root selectNodes {/install/service/url/data[@name="config1"]}]

(or equivalently set nodes [$root selectNodes {//data[@name="config1"]}])

foreach node $nodes {
$node setAttribute name new-value
}

puts [$doc asXML]

George
.



Relevant Pages

  • Re: XML - Find and replace text
    ... certain pieces of text that match a specified pattern? ... know the exact "location" as in the text is always going to be at: ... set xml ... set root [$doc documentElement] ...
    (comp.lang.tcl)
  • Re: XML - Find and replace text
    ... certain pieces of text that match a specified pattern? ... know the exact "location" as in the text is always going to be at: ... Cleanest is probably using XSLT with the tdom package. ...
    (comp.lang.tcl)