如何使用R在XML文档的特定位置添加子节点?

时间:2018-02-14 14:06:15

标签: r xml nodes add

这是我第一次尝试使用R编辑XML。这也是我第一次使用XML文档。我在R中有以下XML文档,其中包含以下代码

XmlDoc <- xmlTreeParse("C:/Users/EQA32292/Downloads/Forecast summary_86_88.xml")
> XmlDoc
$doc
$file
[1] "C:/Users/EQA32292/Downloads/Forecast summary_86_88.xml"

$version
[1] "1.0"

$children
$children$topic
<topic id="ID8980f110-d410-4430-9a63-2c05d93e86cd" xmlns:xt="urn:xpressauthor:trackchanges">
 <prolog>
  <Id>735256</Id>
 </prolog>
 <title>Forecast summary</title>
 <body>
  <h1>Industry Fundamentals</h1>
  <p id="IDe189e620-4ece-455e-be8b-b19ff10e9a2d">SARU is great</p>
  <p id="ID4d2ed6a8-c0af-4743-b9b2-215e0f022baa">SARU IS GOAT</p>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <p id="IDc66e2205-6ade-4956-8d37-38befaffe5ba"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Full width"/>
  <p id="ID66a5a93d-0759-4e29-abfd-5b7fff1d99cf"/>
  <p id="ID49762f56-110c-468f-880f-e9ebc333c22a"/>
  <p id="ID9bfd56f2-9530-4238-8b3e-16a71379c961"/>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Medium width - Right aligned"/>
  <p id="ID6949347b-0e53-4bba-a071-ffe6dd901619"/>
  <p id="ID7192e7fb-b8ad-47dc-8a04-6ba2d2ba7f2e"/>
  <p id="ID30b20f96-e53b-431b-95e2-82ca14507eba"/>
  <p id="IDcb6e086e-3254-4b64-9b3c-4dcd46e0f9b1"/>
  <p id="ID85fcccf1-eb8f-4632-b3a0-299de29fa026"/>
  <p id="ID8cec99ea-0b7e-4edd-a6fa-97ab5238560c"/>
 </body>
 <attachments/>
</topic>


attr(,"class")
[1] "XMLDocumentContent"

$dtd
$external
NULL

$internal
NULL

attr(,"class")
[1] "DTDList"

attr(,"class")
[1] "XMLDocument"         "XMLAbstractDocument"

然后我使用以下代码处理“body”节点上的工作

XMLDocRoot <- xmlRoot(XmlDoc) 
XMLDocRoot
<topic id="ID8980f110-d410-4430-9a63-2c05d93e86cd" xmlns:xt="urn:xpressauthor:trackchanges">
 <prolog>
  <Id>735256</Id>
 </prolog>
 <title>Forecast summary</title>
 <body>
  <h1>Industry Fundamentals</h1>
  <p id="IDe189e620-4ece-455e-be8b-b19ff10e9a2d">SARU is great</p>
  <p id="ID4d2ed6a8-c0af-4743-b9b2-215e0f022baa">SARU IS GOAT</p>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <p id="IDc66e2205-6ade-4956-8d37-38befaffe5ba"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Full width"/>
  <p id="ID66a5a93d-0759-4e29-abfd-5b7fff1d99cf"/>
  <p id="ID49762f56-110c-468f-880f-e9ebc333c22a"/>
  <p id="ID9bfd56f2-9530-4238-8b3e-16a71379c961"/>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Medium width - Right aligned"/>
  <p id="ID6949347b-0e53-4bba-a071-ffe6dd901619"/>
  <p id="ID7192e7fb-b8ad-47dc-8a04-6ba2d2ba7f2e"/>
  <p id="ID30b20f96-e53b-431b-95e2-82ca14507eba"/>
  <p id="IDcb6e086e-3254-4b64-9b3c-4dcd46e0f9b1"/>
  <p id="ID85fcccf1-eb8f-4632-b3a0-299de29fa026"/>
  <p id="ID8cec99ea-0b7e-4edd-a6fa-97ab5238560c"/>
 </body>
 <attachments/>
</topic>

这是我想要添加为“body”节点

的顶级子节点的节点
Newheading <- xmlNode("h2", "Industry Definition")

这就是我一直用作代码

XMLDocRoot[["body"]] <- addChildren(XMLDocRoot[["body"]], Newheading, at = 1)

哪个添加节点但是作为最后一个孩子,而不是第一个

<topic id="ID8980f110-d410-4430-9a63-2c05d93e86cd" xmlns:xt="urn:xpressauthor:trackchanges">
 <prolog>
  <Id>735256</Id>
 </prolog>
 <title>Forecast summary</title>
 <body>
  <h1>Industry Fundamentals</h1>
  <p id="IDe189e620-4ece-455e-be8b-b19ff10e9a2d">SARU is great</p>
  <p id="ID4d2ed6a8-c0af-4743-b9b2-215e0f022baa">SARU IS GOAT</p>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Grouped"/>
  <p id="IDc66e2205-6ade-4956-8d37-38befaffe5ba"/>
  <image imagedef="picture" href="Picture1.png" width="410" height="438" widthdpi="96" heightdpi="96" layout="Full width"/>
  <p id="ID66a5a93d-0759-4e29-abfd-5b7fff1d99cf"/>
  <p id="ID49762f56-110c-468f-880f-e9ebc333c22a"/>
  <p id="ID9bfd56f2-9530-4238-8b3e-16a71379c961"/>
  <image imagedef="picture" href="Picture2.png" width="406" height="436" widthdpi="96" heightdpi="96" layout="Medium width - Right aligned"/>
  <p id="ID6949347b-0e53-4bba-a071-ffe6dd901619"/>
  <p id="ID7192e7fb-b8ad-47dc-8a04-6ba2d2ba7f2e"/>
  <p id="ID30b20f96-e53b-431b-95e2-82ca14507eba"/>
  <p id="IDcb6e086e-3254-4b64-9b3c-4dcd46e0f9b1"/>
  <p id="ID85fcccf1-eb8f-4632-b3a0-299de29fa026"/>
  <p id="ID8cec99ea-0b7e-4edd-a6fa-97ab5238560c"/>
  <h2>Industry Definition</h2>
 </body>
 <attachments/>
</topic>

确实当我跑步时

which(names(xmlChildren(XMLDocRoot[["body"]])) == "h2")
[1] 18

我希望将它作为第一个节点并“推送”所有现有节点。 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

基于此处提供的解决方案(Using R to modify XML node order (or adding a new node into a specific place)) 这完全没问题:

XmlDoc <- xmlTreeParse("C:/Users/EQA32292/Downloads/Forecast summary_86_88.xml")
XMLDocRoot <- xmlRoot(XmlDoc) 
Newheading <- xmlNode("h2", "Industry Definition")
XMLDocRoot[["body"]] <- addChildren(XMLDocRoot[["body"]], Newheading)
MaxNodes <- length(xmlChildren(XMLDocRoot[["body"]]))
MaxNodesMinus1 <- MaxNodes-1
xmlChildren(XMLDocRoot[["body"]]) <- xmlChildren(XMLDocRoot[["body"]])[c(MaxNodes, 1:MaxNodesMinus1)]

正如人们可以看到的那样

which(names(xmlChildren(XMLDocRoot[["body"]])) == "h2")
[1] 1