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]]
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$child1NULL> 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> 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 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. >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]]
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$child1NULL> 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 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 Tick Forecasting: https://eco.bigelow.org/ [[alternative HTML version deleted]]