[R] Removing or overwriting an XML node

Steffen Neumann sneumann at ipb-halle.de
Wed Jul 2 23:29:02 CEST 2008


Hi,

I have an existing XML document on disk, which I'd like 
to use as a template, and exchange a subnode with my own
newly created subtree:

	<?xml version="1.0"?>
	<Duncan>
	  <name a="1" b="xyz">
	    <first>Duncan</first>
	    <last>Temple Lang</last>
	  </name>
	</Duncan>

created by e.g.

	library(XML)
	tr <- xmlTree("Duncan")
	  tr$addTag("name", attrs=c(a=1,b="xyz"), close=F)
	    tr$addTag("first", "Duncan")
	    tr$addTag("last", "Temple Lang")
	tr$closeTag()
	cat(saveXML(tr))

And now imagine I want to change <first>Duncan</first>
into e.g. <initials>D.</initials>. How to do that ?

I am able to add my node:

	library(XML)
	x <-  xmlTreeParse("duncan.xml", useInternalNodes = TRUE)

        # find parent, add as last child:
	name <- getNodeSet(x, "//name")[[1]]
	newXMLNode("initials", "D.", parent=name)

        first <- getNodeSet(x, "//first")[[1]]
	 # wanted: 
	# deleteXMLNode(name)
	# or
	 # replaceXMLNode("initials", "D.", replace=first)

	cat(saveXML(x))
	free(x)

As seen above: how do I get rid of (no offense meant ;-) <first>Duncan</first> ?
Alternatively, how do I overwrite <first>...</first> with my own stuff ? 

I checked the provided examples, and grep'ed the manpages, to no avail.

Thanks in advance, 
yours,
Steffen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080702/9d71da4c/attachment.bin>


More information about the R-help mailing list