[R] Removing or overwriting an XML node

From: Steffen Neumann <sneumann_at_ipb-halle.de>
Date: Wed, 02 Jul 2008 23:29:02 +0200


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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Thu 03 Jul 2008 - 00:07:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jul 2008 - 00:32:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive