[R] how to add a child to a child in XML
David L Carlson
dcarlson at tamu.edu
Thu Mar 22 17:07:07 CET 2018
It is not listed in the table of contents, but as Ben mentioned, it is documented with newXMLDoc (pages 52-58) along with newXMLDoc, newHTMLDoc, newXMLTextNode, newXMLCDataNode, newXMLCommentNoe, newXMLPINode, and newXMLDTDNode on page 53:
newXMLNode(name, ..., attrs = NULL, namespace = character(),
namespaceDefinitions = character(),
doc = NULL, .children = list(...), parent = NULL,
at = NA, cdata = FALSE, suppressNamespaceWarning =
getOption("suppressXMLNamespaceWarning", FALSE),
sibling = NULL, addFinalizer = NA,
noNamespace = length(namespace) == 0 && !missing(namespace),
fixNamespaces = c(dummy = TRUE, default = TRUE))
----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Bond, Stephen
Sent: Thursday, March 22, 2018 9:51 AM
To: 'Ben Tupper' <btupper at bigelow.org>
Cc: 'r-help' <r-help at R-project.org>
Subject: Re: [R] how to add a child to a child in XML
Just to clarify and hopefully catch the attention of the maintainer:
The newXMLNode function is not mentioned in:
https://cran.r-project.org/web/packages/XML/XML.pdf
which supposedly describes all functions in the package.
Stephen
From: Ben Tupper [mailto:btupper at bigelow.org]
Sent: Thursday, March 22, 2018 10:40 AM
To: Bond, Stephen
Cc: r-help
Subject: Re: [R] how to add a child to a child in XML
Hi,
It's a reasonable question. The answer is that it actually is included, but there are many instances across packages where multiple functions are documented on a single help page. The following brings up such a page... (for XML_3.98-1.9)
> library(XML)
> ?newXMLNode
You can see the same on line...
https://www.rdocumentation.org/packages/XML/versions/3.98-1.9/topics/newXMLDoc
You have dig in to find it.
If you are just starting out with XML, you might want to spend some time comparison shopping with the xml2 package. https://www.rdocumentation.org/packages/xml2/versions/1.2.0 I like each one, and I use both XML and xml2 (not at the same time). I have been slowly migrating toward xml2 as I use more of the tidyverse stuff.
Cheers,
Ben
On Mar 22, 2018, at 9:19 AM, Bond, Stephen <Stephen.Bond at cibc.com<mailto:Stephen.Bond at cibc.com>> wrote:
Big thanks. newXMLNode works great. Wonder why it is not included in the documentation.
There is newXMLDoc and newXMLNamespace, but no mention of newXMLNode.
Stephen
From: Ben Tupper [mailto:btupper at bigelow.org]
Sent: Wednesday, March 21, 2018 6:18 PM
To: Bond, Stephen
Cc: r-help
Subject: Re: [R] how to add a child to a child in XML
Hi,
XML doesn't use the `$` to access child nodes. Instead use either `[name]` to get a list of children of that name or `[[name]]` to get the just the first child of that name encountered in the genealogy. Thus for your example...
> root$child1
NULL
> root[['child1']]
<child1 name1="A" name2="B" name3="C"/>
On the other hand, you might consider using newXMLNode() instead of xmlNode() as it accepts a "parent = " argument. The alternative using newXMLNode() would look like...
atts_root <- c("val1","val2","val3")
names(atts_root) <- c("att1","att2","att3") root <- newXMLNode("root", attrs = atts_root)
atts_child <- LETTERS[1:3]
names(atts_child) <- paste("name",1:3,sep="") child <- newXMLNode("child",attrs = atts_child, parent = root)
atts_grandchild <- letters[1:3]
names(atts_grandchild) <- paste("name",4:6,sep="") grandchild <- newXMLNode("grandchild",attrs = atts_grandchild, parent = child)
root
#<root att1="val1" att2="val2" att3="val3"> # <child name1="A" name2="B" name3="C">
# <grandchild name4="a" name5="b" name6="c"/>
# </child>
#</root>
Cheers,
Ben
On Mar 21, 2018, at 4:25 PM, Bond, Stephen <Stephen.Bond at cibc.com<mailto:Stephen.Bond at cibc.com>> wrote:
I am trying to add a child to a child using XML package in R. the following fails
library(XML)
node1 <- c("val1","val2","val3")
names(node1) <- c("att1","att2","att3")
root <- xmlNode("root", attrs=node1)
node2 <- LETTERS[1:3]
names(node2) <- paste("name",1:3,sep="")
root <- addChildren(root,xmlNode("child1",attrs=node2))
node3 <- letters[1:3]
names(node3) <- paste("name",4:6,sep="")
root <- addChildren(root$child1,xmlNode("child2",attrs=node3))
Error in UseMethod("addChildren") : no applicable method for 'addChildren' applied to an object of class "NULL"
Stephen B
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org<http://www.bigelow.org/>
Tick Forecasting: https://eco.bigelow.org/
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Tick Forecasting: https://eco.bigelow.org/
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
More information about the R-help
mailing list