This is the first time I am trying to edit XML using R. This is also the first time I am working with XML document. I have the following XML document in R with following code
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"
I then use the following code the work on the "body" node
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>
This is the node I want to add as the top child for the "body" node
Newheading <- xmlNode("h2", "Industry Definition")
This is what I have been using as a code
XMLDocRoot[["body"]] <- addChildren(XMLDocRoot[["body"]], Newheading, at = 1)
Which does add the node but as the last child, not the first
<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>
Indeed when I run
which(names(xmlChildren(XMLDocRoot[["body"]])) == "h2")
[1] 18
I would like to have it as the first node and "push" all the existing node down. Thanks a lot for your help!