[R] how to add a child to a child in XML
Ben Tupper
btupper at bigelow.org
Thu Mar 22 15:39:32 CET 2018
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 <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> 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 <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/ <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]]
More information about the R-help
mailing list