[英]How to add a child node at a specific location of an XML document using R?
這是我第一次嘗試使用 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
我想將它作為第一個節點並“推”所有現有節點。 非常感謝你的幫助!
基於此處提供的解決方案( 使用 R 修改 XML 節點順序(或將新節點添加到特定位置) )這完全正常:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.