I'm trying to create an RSS document using R's XML package, but I'm running into trouble. Here is the code I'm using:
df <- data.frame(Labels <- c("Label_1"),
Values <- c("Value_1")
)
# CREATE XML FILE
doc = newXMLDoc()
root = newXMLNode("rss", doc = doc)
# WRITE XML NODES AND DATA
channel = newXMLNode("channel", parent = root)
title = newXMLNode("title","Metrics", parent = channel)
for (i in 1:nrow(df)){
prodNode = newXMLNode("Metric", parent = channel)
# APPEND TO PRODUCT NODE
newXMLNode("description", df$Labels[i], parent = prodNode)
newXMLNode("item", df$Values[i], parent = prodNode)
}
# OUTPUT XML CONTENT TO CONSOLE
print(doc)
# OUTPUT XML CONTENT TO FILE
saveXML(doc, file="RSS_Output.xml")
This gives me the following output, which doesn't work with RSS parsers because of, among other things, the wrong root node. Any ideas how to more cleanly generate an RSS file?
<?xml version="1.0"?>
<rss>
<channel>
<title>Metrics</title>
<Metric>
<description>Label_1</description>
<item>Value_1</item>
</Metric>
</channel>
</rss>
question from:
https://stackoverflow.com/questions/65892047/how-to-use-rs-xml-package-to-write-an-xml-document-in-rss-format 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…