<doc xmlns:r="http://www.r-project.org">
 <a>
   <b id="1"/>
   <b id="2"/>
   <c/>
   <d/>
   <b id="3"/>
   <b id="4"/>
   <c/>
 </a>

<r:code>
doc = xmlInternalTreeParse("examples/reorder.xml")
 bs = getNodeSet(doc, "//b")
length(bs)
# [1] 4
xmlName(xmlRoot(doc)[[1]])
# [1] "a"
a = xmlRoot(doc)[[1]]
addChildren(a, kids = bs)
saveXML(doc)

addChildren(a, kids = bs, at = 0)
</r:code>
</doc>