简体   繁体   中英

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 to a paucity of time) I decided to split the files, but simply concatenate the split files by using the following code

    var xmlDoc;
    xmlhttp.open("GET","distributome-relations.xml",false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;
    xmlhttp=createAjaxRequest();
    xmlhttp.open("GET","distributome-references.xml",false);
    xmlhttp.send();
    xmlDoc = xmlDoc+xmlhttp.responseXML;
            try{
        DistributomeXML_Objects=xmlDoc.documentElement.childNodes;
    }catch(error){
        DistributomeXML_Objects=xmlDoc.childNodes;
    }

Which doesn't seem to work while the original code

xmlhttp.open("GET","Distributome.xml",false);
    xmlhttp.send();
    if (!xmlhttp.responseXML.documentElement && xmlhttp.responseStream)
        xmlhttp.responseXML.load(xmlhttp.responseStream);
    xmlDoc = xmlhttp.responseXML;
    try{
        DistributomeXML_Objects=xmlDoc.documentElement.childNodes;
    }catch(error){
        DistributomeXML_Objects=xmlDoc.childNodes;
    }

Works perfectly fine.

I'm not sure how to proceed with this.

I have simply split the file

http://www.distributome.avinashc.com/avinash/Distributome.xml into

http://www.distributome.avinashc.com/avinash/distributome-relations.xml and http://www.distributome.avinashc.com/avinash/distributome-references.xml

The last two files will appear improperly formatted because only a simple concatenation of the two files is expected to be a valid XML document.

I suspect that this is due to the use of the responseXML method and that I should use an alternate method.

Is there a better way to do this. I would welcome any suggestions, and of course - answers to my original question.

Thanks

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 responseXML over 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM