如何正确地从.csv创建XML?

时间:2016-08-31 10:09:05

标签: r

我想从XML文件创建一个.csv文件。我有一些难以获得所需的结构:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <employee ID="1">
    <Name>Steve</Name>
    <City>Boston</City>
    <Age>33</Age>
  </employee>
  <employee ID="2">
    <Name>Michael</Name>
    <City>Dallas</City>
    <Age>45</Age>
  </employee>
  <employee ID="3">
    <Name>John</Name>
    <City>New York</City>
    <Age>89</Age>
  </employee>
  <employee ID="4">
    <Name>Thomas</Name>
    <City>LA</City>
    <Age>62</Age>
  </employee>
   <employee ID="5">
    <Name>Clint</Name>
    <City>Paris</City>
    <Age>30</Age>
  </employee>
</document>

我尝试过:

library(XML)

# Some data
df <-
  read.csv(textConnection('"ID","Name","City","Age"
"1","Steve","Boston",33
"2","Michael","Dallas",45
"3","John","New York",89
"4","Thomas","LA",62
 "5","Clint","Paris",30'),
       as.is=TRUE)

xml <- xmlTree()
xml$addTag("document", close=FALSE)
for (i in 1:nrow(df)) {
  xml$addTag("employee", close=FALSE)
  for (j in names(df)) {
    xml$addTag(j, df[i, j])
  }
  xml$closeTag()
}
xml$closeTag()

看起来几乎符合要求,但ID在employee之下,而不是在同一行,且编码不在标题中:

<?xml version="1.0"?>

<document>
  <employee>
    <ID>1</ID>
    <Name>Steve</Name>
    <City>Boston</City>
    <Age>33</Age>
  </employee>
  <employee>
    <ID>2</ID>
    <Name>Michael</Name>
    <City>Dallas</City>
    <Age>45</Age>
  </employee>
  <employee>
    <ID>3</ID>
    <Name>John</Name>
    <City>New York</City>
    <Age>89</Age>
  </employee>
  <employee>
    <ID>4</ID>
    <Name>Thomas</Name>
    <City>LA</City>
    <Age>62</Age>
  </employee>
  <employee>
    <ID>5</ID>
    <Name>Clint</Name>
    <City>Paris</City>
    <Age>30</Age>
   </employee>
 </document>

1 个答案:

答案 0 :(得分:2)

使用addNode代替addTag。它们是相同的

> identical(xml$addTag, xml$addNode)
[1] TRUE

所以这是一个偏好的问题。您可以提供attrs参数来添加ID属性。您可以在保存文件时添加编码:

library(XML)
df <-
  read.csv(textConnection('"ID","Name","City","Age"
                          "1","Steve","Boston",33
                          "2","Michael","Dallas",45
                          "3","John","New York",89
                          "4","Thomas","LA",62
                          "5","Clint","Paris",30'),
           as.is=TRUE)

xml <- xmlTree("document")
for (i in 1:nrow(df)) {
  xml$addNode("employee", attrs = c(ID = df[i,"ID"]), close = FALSE)
  appNames <- names(df)[names(df) != "ID"]
  for (j in appNames) {
    xml$addNode(j, df[i, j])
  }
  xml$closeNode()
}
xml$closeNode()
saveXML(xml$doc(), "text.xml", encoding = "UTF-8")
xmlParse("text.xml")
<?xml version="1.0" encoding="UTF-8"?>
<document>
  <employee ID="1">
    <Name>Steve</Name>
    <City>Boston</City>
    <Age>33</Age>
  </employee>
  <employee ID="2">
    <Name>Michael</Name>
    <City>Dallas</City>
    <Age>45</Age>
  </employee>
  <employee ID="3">
    <Name>John</Name>
    <City>New York</City>
    <Age>89</Age>
  </employee>
  <employee ID="4">
    <Name>Thomas</Name>
    <City>LA</City>
    <Age>62</Age>
  </employee>
  <employee ID="5">
    <Name>Clint</Name>
    <City>Paris</City>
    <Age>30</Age>
  </employee>
</document>