从xml中取内容 本帖最后由 geelpheels 于 2010-08-21 18:12:44 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script>function requestHttp(url, type, data, loaded, error) { if (typeof loaded != "function") return; var xmlhttp = typeof XMLHttpRequest == "undefined" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) loaded(xmlhttp); else if (parseFloat(xmlhttp.status) > 300 && typeof error == "function") error(xmlhttp); } xmlhttp.open(typeof type == "string" ? type : "GET", url, true); if (typeof data == "string") { xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-Length", data.length); } xmlhttp.send(data);}requestHttp("data.xml", "GET", null, function(xmlhttp) { if (!xmlhttp.responseXML) return; var xmldoc = xmlhttp.responseXML.documentElement; if (xmldoc.tagName != "all") return; var datas = []; var msg = []; // 测试输出用 for (var i = 0; i < xmldoc.childNodes.length; i++) { var item = xmldoc.childNodes[i]; if (item.tagName != "item") continue; var data = {}; for (var j = 0; j < item.childNodes.length; j++) { switch (item.childNodes[j].tagName) { case "id": var node = item.childNodes[j]; if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3) data.id = node.childNodes[0].nodeValue; break; case "name": var node = item.childNodes[j]; if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3) data.name = node.childNodes[0].nodeValue; break; } } datas.push(data); msg.push([data.id, data.name].join("=")); } alert(msg.join("\n"));});</script> <html><head><script type="text/javascript">function load(){ var xmlDoc = new ActiveXObject('Microsoft.XMLDOM'); xmlDoc.async = false; xmlDoc.load("a.xml"); var doc = xmlDoc.documentElement; var a = doc.childNodes[0].childNodes[0]; alert(a.getAttribute("value"));}</script></head><body onload="load()"></body></html>添加节点XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load(""); XmlNode root=xmlDoc.SelectSingleNode("sList");XmlElement xe1=xmlDoc.CreateElement("s"); XmlElement xesub1=xmlDoc.CreateElement("key"); xesub1.InnerText=""; xe1.AppendChild(xesub1);root.AppendChild(xe1);xmlDoc.Save(""); 大神!为什么我的<body>里面的<img >图片显示不出来呢,难道是下面的js函数问题? 怎么触发textarea里文本显示 大家帮我看看这段JavaScript代码错在哪? 如何关闭父层div JS赋值问题? 论坛中如何实现点某条文章的图标后显示出它的所有回复 如何让单选框效果好一些? 怎样用代码控制select选择某一项 请问IE打印时如何设置pagesize 为什么打开时窗口不能是满屏。。大家帮我改一下 禁止输入的字符中含有-- select标签如何实现按输入字符自动选择与首字母匹配的项
function requestHttp(url, type, data, loaded, error) {
if (typeof loaded != "function") return;
var xmlhttp = typeof XMLHttpRequest == "undefined" ?
new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
if (xmlhttp.status == 200)
loaded(xmlhttp);
else if (parseFloat(xmlhttp.status) > 300 && typeof error == "function")
error(xmlhttp);
}
xmlhttp.open(typeof type == "string" ? type : "GET", url, true);
if (typeof data == "string") {
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Length", data.length);
}
xmlhttp.send(data);
}requestHttp("data.xml", "GET", null, function(xmlhttp) {
if (!xmlhttp.responseXML) return;
var xmldoc = xmlhttp.responseXML.documentElement;
if (xmldoc.tagName != "all") return;
var datas = [];
var msg = []; // 测试输出用
for (var i = 0; i < xmldoc.childNodes.length; i++) {
var item = xmldoc.childNodes[i];
if (item.tagName != "item") continue;
var data = {};
for (var j = 0; j < item.childNodes.length; j++) {
switch (item.childNodes[j].tagName) {
case "id":
var node = item.childNodes[j];
if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3)
data.id = node.childNodes[0].nodeValue;
break;
case "name":
var node = item.childNodes[j];
if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3)
data.name = node.childNodes[0].nodeValue;
break;
}
}
datas.push(data);
msg.push([data.id, data.name].join("="));
}
alert(msg.join("\n"));
});
</script>
<head>
<script type="text/javascript">
function load(){
var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load("a.xml");
var doc = xmlDoc.documentElement;
var a = doc.childNodes[0].childNodes[0];
alert(a.getAttribute("value"));
}
</script>
</head>
<body onload="load()">
</body>
</html>添加节点
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("");
XmlNode root=xmlDoc.SelectSingleNode("sList");
XmlElement xe1=xmlDoc.CreateElement("s"); XmlElement xesub1=xmlDoc.CreateElement("key");
xesub1.InnerText="";
xe1.AppendChild(xesub1);
root.AppendChild(xe1);
xmlDoc.Save("");