Re: XML - Find and replace text





package require tdom

set xml {<install>
<service>
...
</service>
<service>
<url>
<data name="config1">this is what I want to change</data>
</url>
</service>
</install>
}
set doc [dom parse $xml]
set root [$doc documentElement]

set textNode [$root selectNodes {service/url/data[@name="config1"]/text()}]

$textNode nodeValue "New text"

puts [$root asXML]

You can use many variations of xpath depending on your constraints:


set textNode [$root selectNodes {//data[@name="config1"]/text()}]



gcgaim@xxxxxxxxx escribió:
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!

.