R XML - Error attemping to add Node to Internal Node

rolleman

New Member
I am reading an XML file using R and the XML package. It is my wish to read in the XML document, perform some stat calculations and insert the results back into the XML document as a child node and then save the updated XML file to a new location.Here's an illustrative example. The XML file I'm reading (insert_node_error.xml):\[code\]<library> <book> <title>Book1</title> <author>AuthorA</author> </book> <book> <title>Book2</title> <author>AuthorB</author> </book> </library>\[/code\]Here is the code that I'm running:\[code\]#load filefile.name <- "xml\\insert_node_error.xml"input.xml <- xmlInternalTreeParse(file.name)#produce list of books in library (my actual code has loops and calcs here)books.xml <- getNodeSet(input.xml, "//book")#set price for first book as "price" nodeprice.xml <- xmlNode("price", 19.99)#attempt to insert that price as child within the first book nodebooks.xml[[1]] <- addChildren(books.xml[[1]], price.xml)\[/code\]The output has appended the node but has stripped all the XML out of it and is supplying only text.\[code\]> input.xml<?xml version="1.0"?><library> <book><title>Book1</title><author>AuthorA</author>pricetext19.99</book> <book> <title>Book2</title> <author>AuthorB</author> </book></library>\[/code\]I would like to see:\[code\]<library> <book> <title>Book1</title> <author>AuthorA</author> <price>19.99</price> </book> <book> <title>Book2</title> <author>AuthorB</author> </book> </library>\[/code\]Any suggestions?
 
Back
Top