admin管理员组文章数量:1405340
I seem to be stuck in implementing an "XML data insert using Javascript".
My application is an asp with asp AJAX.
My XML document is as follows:
<?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>
This is the JavaScript function I use to try and insert data in my XML file:
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);
}
The XML file is sitting on the root folder of my project, where pages are located.
I don't know why it doesn't work.
============================== I have now changed the code as follows, but still no effect.
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);
}
I seem to be stuck in implementing an "XML data insert using Javascript".
My application is an asp with asp AJAX.
My XML document is as follows:
<?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>
This is the JavaScript function I use to try and insert data in my XML file:
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);
}
The XML file is sitting on the root folder of my project, where pages are located.
I don't know why it doesn't work.
============================== I have now changed the code as follows, but still no effect.
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);
}
Share
Improve this question
edited Jan 28, 2016 at 9:03
User42
9651 gold badge16 silver badges28 bronze badges
asked Dec 14, 2011 at 20:21
Lindelani ZwaneLindelani Zwane
311 gold badge1 silver badge7 bronze badges
1
-
1
You are still doing
xml.responseXML.appendChild
. You cannot append to the document itself, as mentioned in both answers below. – Wayne Commented Dec 14, 2011 at 23:54
3 Answers
Reset to default 2You're currently invoking send
and then expecting the response to be immediately available. This is fundamentally wrong. The third argument to xml.open
(true
) indicates that you want the request to be executed asynchronously. You need to handle the response in an xml.onreadystatechange
callback:
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);
}
Note that I also changed xml.responseXML.appendChild
to xml.responseXML.documentElement.appendChild
as pointed out by @Dr.Molle. You can't append to the document itself.
Try
xml.responseXML.documentElement.appendChild(hotel);
Currently you append the node to the document, what is an illegal operation because an XML-document may only have 1 root-element.
xml.responseXML.documentElement
points to the root-element(<hotels/>
)
I would say that you are not saving the changes made. You need to save it to the XML file. You could first take the response XML as variable xmldoc, then add the new data into it and at last write- xmldoc.save("hotelrates.xml");
本文标签: Inserting an XML node using JavascriptStack Overflow
版权声明:本文标题:Inserting an XML node using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744895967a2631065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论