devxlogo

Insert XML Into Another XML Document

Insert XML Into Another XML Document

Question:

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.

Answer:

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 parentDocdim childDocdim insertionNodeset 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist