使用Javascript插入XML节点

使用Javascript插入XML节点,javascript,xml,Javascript,Xml,我似乎被困在实现“使用Javascript插入XML数据”中 我的应用程序是一个带有asp.net AJAX的asp.net 我的XML文档如下所示: <?xml version="1.0" encoding="utf-8" ?> <hotels> <hotel supplier="Sarova panafric" id="HTL-10001"> <supplier>Sarova panafric</supplier&

我似乎被困在实现“使用Javascript插入XML数据”中

我的应用程序是一个带有asp.net AJAX的asp.net

我的XML文档如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<hotels>
    <hotel supplier="Sarova panafric" id="HTL-10001">
        <supplier>Sarova panafric</supplier>
        <contact>Mr S Njoroge</contact>
        <tel>75-525254</tel>
    </hotel>
    <hotel supplier="Sarova mara" id="HTL-10002">
        <supplier>Sarova mara</supplier>
        <contact>Mr ole seni</contact>
        <tel>20-54574</tel>
    </hotel>
</hotels>
XML文件位于我的项目的根文件夹中,页面位于该文件夹中

我不知道为什么它不起作用

============================== 我现在已经修改了代码如下,但仍然没有效果

函数addhotels(){
var-xml;
if(window.ActiveXObject){
xml=新的ActiveXObject(“Microsoft.XMLHTTP”);
}否则{
xml=新的XMLHttpRequest();
}
open(“GET”,“hotelRates.xml”,true);
send(null);
var hotels=xml.responseXML.createElement(“hotels”);
var supplier=xml.responseXML.createElement(“供应商”);
supplier.appendChild(xml.responseXML.createTextNode(“Sarova stanley”);
var contact=xml.responseXML.createElement(“contact”);
appendChild(xml.responseXML.createTextNode(“Mr Njoroge”);
var tel=xml.responseXML.createElement(“tel”);
tel.appendChild(xml.responseXML.createTextNode(“21454741”);
酒店。appendChild(供应商);
酒店.儿童旅馆(联系人);
佩德柴尔德酒店(电话);
xml.responseXML.appendChild(酒店);
}
试试看

当前,您将节点附加到文档中,这是一个非法操作,因为XML文档可能只有一个根元素。

xml.responseXML.documentElement
指向根元素(

您当前正在调用的
send
,然后期望响应立即可用。这是根本错误的。
xml.open
true
)的第三个参数表示希望异步执行请求。您需要在
xml.onreadystatechange
回调中处理响应:

function addhotels() {
    var xml;
    if (window.ActiveXObject) {
        xml = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        xml = new XMLHttpRequest();
    }
    xml.onreadystatechange = function() {
        if (xml.readyState == 4 && xml.status == 200) {
            var resp = xml.responseXML;
            var hotel = xml.responseXML.createElement("hotel");

            var supplier = xml.responseXML.createElement("supplier");
            supplier.appendChild(xml.responseXML
                    .createTextNode("Sarova stanley"));

            var contact = xml.responseXML.createElement("contact");
            contact.appendChild(xml.responseXML.createTextNode("Mr Njoroge"));

            var tel = xml.responseXML.createElement("tel");
            tel.appendChild(xml.responseXML.createTextNode("21454741"));

            hotel.appendChild(supplier);
            hotel.appendChild(contact);
            hotel.appendChild(tel);
            xml.responseXML.documentElement.appendChild(hotel);
        }
    }
    xml.open("GET", "hotelRates.xml", true);
    xml.send(null);
}

请注意,正如@Dr.Molle所指出的,我还将
xml.responseXML.appendChild
更改为
xml.responseXML.documentElement.appendChild
。您不能附加到文档本身。

我认为您没有保存所做的更改。您需要将其保存到XML文件中。您可以首先将响应XML作为变量xmldoc,然后将新数据添加到其中,最后写入-xmldoc.save(“hotelrates.XML”)

一个大问题是,他正在启动一个异步请求,然后期望响应立即准备好……哦,是的,我没有看这个。但是,当他解决这个问题时,他必须考虑我的回答。谢谢你,我会尽力通知你结果。你仍然在做代码> XML。您不能附加到文档本身,如下面两个答案中所述。
function addhotels() {
    var xml;
    if (window.ActiveXObject) {
        xml = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        xml = new XMLHttpRequest();
    }
    xml.onreadystatechange = function() {
        if (xml.readyState == 4 && xml.status == 200) {
            var resp = xml.responseXML;
            var hotel = xml.responseXML.createElement("hotel");

            var supplier = xml.responseXML.createElement("supplier");
            supplier.appendChild(xml.responseXML
                    .createTextNode("Sarova stanley"));

            var contact = xml.responseXML.createElement("contact");
            contact.appendChild(xml.responseXML.createTextNode("Mr Njoroge"));

            var tel = xml.responseXML.createElement("tel");
            tel.appendChild(xml.responseXML.createTextNode("21454741"));

            hotel.appendChild(supplier);
            hotel.appendChild(contact);
            hotel.appendChild(tel);
            xml.responseXML.documentElement.appendChild(hotel);
        }
    }
    xml.open("GET", "hotelRates.xml", true);
    xml.send(null);
}