0
我已經成功獲取示例1 xml作爲R中的數據框對象,但遇到示例2的麻煩。有沒有人對R代碼將數據從mtcars.xml轉換爲數據框有所建議?在R中解析xml - 返回數據框對象
實施例1)
library(XML)
# Save the URL of the xml file in a variable
xml.url <- "http://www.w3schools.com/xml/plant_catalog.xml"
# Use the xmlTreePares-function to parse xml file directly from the web
xmlfile <- xmlTreeParse(xml.url)
# Use the xmlRoot-function to access the top node
xmltop = xmlRoot(xmlfile)
# have a look at the XML-code of the first subnodes:
print(xmltop)[1:2]
# To extract the XML-values from the document, use xmlSApply:
plantcat <- xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue))
示例2)
library(XML)
# Save the URL of the xml file in a variable
doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML"))
xmlfile <- xmlTreeParse(doc)
# Use the xmlRoot-function to access the top node
xmltop = xmlRoot(xmlfile)
# have a look at the XML-code of the first subnodes:
print(xmltop)[1:2]
# To extract the XML-values from the document, use xmlSApply:
mtcarscat <- xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue))
對於第一個,'xmlToDataFrame('http://www.w3schools.com/xml/plant_catalog.xml')'一氣呵成。 – alistaire