gpt4 book ai didi

javascript - 使用 Javascript 插入 XML 节点

转载 作者:行者123 更新时间:2023-11-30 13:25:51 25 4
gpt4 key购买 nike

我似乎被困在实现“使用 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 文件中插入数据的 JavaScript 函数:

function addhotels() {
var xml;
if (window.ActiveXObject) {
xml = new ActiveXObject("Microsoft.XMLHTTP");
} else {
xml = new XMLHttpRequest();
}
xml.open("GET", "hotelRates.xml", true);
xml.send(null);

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.appendChild(hotel);
}

XML 文件位于我的项目的根文件夹中,页面所在的位置。

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

==============================我现在改了代码如下,还是没有效果。

function addhotels() {
var xml;
if (window.ActiveXObject) {
xml = new ActiveXObject("Microsoft.XMLHTTP");
} else {
xml = new XMLHttpRequest();
}
xml.open("GET", "hotelRates.xml", true);
xml.send(null);

var hotels = xml.responseXML.createElement("hotels");

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"));

hotels.appendChild(supplier);
hotels.appendChild(contact);
hotels.appendChild(tel);
xml.responseXML.appendChild(hotels);
}

最佳答案

您当前正在调用 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。您不能附加到文档本身。

关于javascript - 使用 Javascript 插入 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8511104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com