Re: XML - Find and replace text
- From: Ramon Ribó <ramsan@xxxxxxxxxxxxx>
- Date: Wed, 19 Sep 2007 18:13:25 +0200
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:Cleanest is probably using XSLT with the tdom package.What would be the cleanest way to parse an XML file and replaceI'm using ActiveTcl if that makes any difference.
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!
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!
- References:
- XML - Find and replace text
- From: gcgaim
- Re: XML - Find and replace text
- From: gcgaim
- Re: XML - Find and replace text
- From: schlenk
- Re: XML - Find and replace text
- From: gcgaim
- XML - Find and replace text
- Prev by Date: Re: tcl's byte code framework an bytecode interpreter
- Next by Date: Re: Scilab and Tcl/Tk
- Previous by thread: Re: XML - Find and replace text
- Next by thread: Re: XML - Find and replace text
- Index(es):