R 使用xml2向节点添加子节点

R 使用xml2向节点添加子节点,r,xml2,R,Xml2,我觉得这很容易,但我就是不能让它工作 假设我有这个节点 {xml_document} <var ID="V512" name="supqad1" files="F1" dcml="0" intrvl="discrete"> [1] <location width="1"/> [2] <labl>\nAdministration of supplementary questionnaire 1\n</labl> [3] <valrng&g

我觉得这很容易,但我就是不能让它工作

假设我有这个节点

{xml_document}
<var ID="V512" name="supqad1" files="F1" dcml="0" intrvl="discrete">
 [1] <location width="1"/>
 [2] <labl>\nAdministration of supplementary questionnaire 1\n</labl>
 [3] <valrng>\n  <range min="1" max="3"/>\n</valrng>
 [4] <invalrng>\n  <range min="6" max="9"/>\n</invalrng>
 [5] <sumStat type="vald">\n18644\n</sumStat>
 [6] <sumStat type="invd">\n24356\n</sumStat>
 [7] <catgry>\n  <catValu>\n1\n</catValu>\n  <labl>\nCompleted by respondent, no help from you\n</labl>\n</catgry>
 [8] <catgry>\n  <catValu>\n2\n</catValu>\n  <labl>\nCompleted by respondent, some help from you\n</labl>\n</catgry>
 [9] <catgry>\n  <catValu>\n3\n</catValu>\n  <labl>\nFace to face interview\n</labl>\n</catgry>
[10] <catgry missing="Y">\n  <catValu>\n6\n</catValu>\n  <labl>\nNot applicable\n</labl>\n</catgry>
[11] <catgry missing="Y">\n  <catValu>\n9\n</catValu>\n  <labl>\nNo answer\n</labl>\n</catgry>
[12] <varFormat type="numeric" schema="other"/>

您可以像这样添加新节点:

my_xml <- read_xml("<fruits><apple/><banana/><grape/></fruits>")

my_xml
{xml_document}
<fruits>
[1] <apple/>
[2] <banana/>
[3] <grape/>

my_xml %>% 
  xml_add_child("whatever") %>%
  xml_add_child("other", "hello")

my_xml
{xml_document}
<fruits>
[1] <apple/>
[2] <banana/>
[3] <grape/>
[4] <whatever>\n  <other>hello</other>\n</whatever>
my_xml%
xml_添加_子项(“任意”)%%>%
xml\u add\u child(“其他”、“你好”)
我的xml
{xml_文档}
[1] 
[2] 
[3] 
[4] \n您好\n

作为替代方案,遵循
xml2
渐晕图,使用与Steven相同的示例:

myu-xml
my_xml <- read_xml("<fruits><apple/><banana/><grape/></fruits>")

my_xml
{xml_document}
<fruits>
[1] <apple/>
[2] <banana/>
[3] <grape/>

my_xml %>% 
  xml_add_child("whatever") %>%
  xml_add_child("other", "hello")

my_xml
{xml_document}
<fruits>
[1] <apple/>
[2] <banana/>
[3] <grape/>
[4] <whatever>\n  <other>hello</other>\n</whatever>