Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: XML
Expertise: Beginner
Jan 21, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Insert XML Into Another XML Document


Is there an easy and efficient way to insert an XML document into another XML template? The insertion point can be before, between, or after specified nodes.


Sure. I do this all the time. If you're going from a DOM-based solution, you can incorporate one XML structure into another through the use of appendChild and cloneNode. For example:

dim parentDoc
dim childDoc
dim insertionNode
set parentDoc=createObject("Microsoft.XMLDOM")
set childDoc=createObject("Microsoft.XMLDOM")
parentDoc.load "myXMLDoc.xml"
childDoc.load "myChildXMLDoc.xml"
set insertionNode=parentDoc.selectSingleNode("//some/arbitrary/path")
insertionNode.appendChild childDoc.documentElement

In this case, you load in two documents, select the node you want to attach the child document to, then append the document element of the child document as a child to that node. There is similarly an .insertBefore(nodePos) method that can insert the new document into the node along with any other children that the node happens to have.

DevX Pro
Thanks for your registration, follow us on our social networks to keep up-to-date