Skip to content Skip to sidebar Skip to footer

Concatenating Two Xml Files Using Javascript/ajax

I have been tasked with splitting an XML file into two parts for ease of editing. But all the existing codebase is designed to treat it as a single file. As a middle ground (due t

Solution 1:

It would help if you explained what doesn't seem to work translates to but from a quick look at your code I can tell that you are trying to concatenate two DOM Document objects using +. This line:

xmlDoc = xmlDoc + xmlhttp.responseXML;

The responseXML property of the XHR doesn't return a string like you seem to expect (btw, concatenating two XML files like that would very likely result into a not well formed XML anyway). The second example treats it properly for what it is - the DOM Document object.

You can read more about responseXMLover at MDN. Pay attention to the note about the Content-Type and how you might need to use the overrideMimeType() on your XHR to make it believe it received an XML from the server if the server didn't set the header properly.

To do your concatenation in the XML-aware fashion you would need to grab the root node from the second document (document.documentElement), import it into the first document using the importNode() and then append it as a child node where you want it to be using appendChild()

Post a Comment for "Concatenating Two Xml Files Using Javascript/ajax"