使用JavaScript / AJAX连接两个XML文件 [英] Concatenating two XML files using JavaScript/AJAX

查看:140
本文介绍了使用JavaScript / AJAX连接两个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是将XML文件分成两部分,以方便编辑。

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;
    }

当原始代码

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;
    }

工作完全正常。

我不知道如何继续这个。

I'm not sure how to proceed with this.

我只需拆分文件

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

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

最后两个文件的格式格式不正确,因为这两个文件只能简单连接是一个有效的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.

我怀疑这是由于使用了responseXML方法,我应该使用一种替代方法。

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.

谢谢

推荐答案

如果您解释了什么似乎没有使用,则可以帮助,但从快速查看代码可以看出,您正在尝试连接两个 DOM文档 对象使用 + 。这行:

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;

XHR的 responseXML 属性没有' t返回一个像你似乎期望的字符串(btw,连接这样的两个XML文件很可能会导致一个格式不正确的XML)。第二个例子正确对待它是什么 - DOM Document对象。

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.

您可以阅读更多关于 responseXML 在MDN上 。请注意关于 Content-Type 的注意事项,以及您可能需要如何使用XHR上的 overrideMimeType()如果服务器未正确设置标题,则认为它从服务器接收到XML。

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.

要以XML感知方式进行连接,您将需要从第二个文档抓取根节点( document.documentElement ),使用 importNode() ,然后将其作为您想要使用的子节点附加 appendChild()

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

这篇关于使用JavaScript / AJAX连接两个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆