【发布时间】:2018-02-14 14:06:15
【问题描述】:
这是我第一次尝试使用 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
我想将它作为第一个节点并“推”下所有现有节点。 非常感谢您的帮助!
【问题讨论】:
-
很可能是重复的 (stackoverflow.com/questions/33478055/…)