Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R在XML节点中插入序列号_R_Xml - Fatal编程技术网

使用R在XML节点中插入序列号

使用R在XML节点中插入序列号,r,xml,R,Xml,我有这样一个xml文件: <?xml version="1.0"?> <document> <row> <ID>49414</ID> <Check>1</Check> <Date>27/3/2019 0:00:00</Date> </row> <row> <ID>49643</ID&

我有这样一个xml文件:

<?xml version="1.0"?>
<document>
  <row> 
    <ID>49414</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>
  <row>
    <ID>49643</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>

…
是否有任何直接的方法可以使用R执行此任务? 提前谢谢

我尝试过的

我已经检查了这个问题的答案,但是当我应用
xpathApply
时,我不会得到
行的空值,而是兄弟节点的折叠值:

> x<-xmlParse('mydata.xml')
> xmlValue(xpathApply(x,"//row")[[1]])
[1] "49414127/3/2019 0:00:00"
>x xmlValue(xpathApply(x,//行)[[1]]
[1] "49414127/3/2019 0:00:00"
这很有效

library(xml2)
library(dplyr)

x <- read_xml('mydata.xml')

# the following modifies nodes in x
x %>%
  xml_find_all('./row') %>%
  xml_set_attr('itemID', seq_along(.)) 

x %>%
  write_xml('mydata2.xml')
库(xml2)
图书馆(dplyr)
x%
xml_find_all('./行')%>%
xml\u set\u attr('itemID',seq\u沿线(.))
x%>%
写入xml('mydata2.xml')
那么“mydata2.xml”包含:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <row itemID="1">
    <ID>49414</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>
  <row itemID="2">
    <ID>49643</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>
</document>

49414
1.
27/3/2019 0:00:00
49643
1.
27/3/2019 0:00:00
这很有效

library(xml2)
library(dplyr)

x <- read_xml('mydata.xml')

# the following modifies nodes in x
x %>%
  xml_find_all('./row') %>%
  xml_set_attr('itemID', seq_along(.)) 

x %>%
  write_xml('mydata2.xml')
库(xml2)
图书馆(dplyr)
x%
xml_find_all('./行')%>%
xml\u set\u attr('itemID',seq\u沿线(.))
x%>%
写入xml('mydata2.xml')
那么“mydata2.xml”包含:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <row itemID="1">
    <ID>49414</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>
  <row itemID="2">
    <ID>49643</ID>
    <Check>1</Check>
    <Date>27/3/2019 0:00:00</Date>
  </row>
</document>

49414
1.
27/3/2019 0:00:00
49643
1.
27/3/2019 0:00:00