IE下正常,火狐等浏览器读取失败。代码如下:
JS代码:
<script type="text/javascript">
window.onload = function () {
HoneyMoon();
}
function HoneyMoon() {
var XMLDoc;
var Browse = "";
var XMLFile = "xml/test.xml"
if (window.ActiveXObject) {
XMLDoc = new ActiveXObject('Msxml2.DOMDocument');
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ie";
}
else if (document.implementation && document.implementation.createDocument) {
XMLDoc = document.implementation.createDocument("", "", null);
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ff";
}
else {
alert('您所使用的浏览器不兼容部分JavaScript功能!请使用IE、Firefox、Opera等浏览器。');
}
//根据浏览器不同执行不同显示
if (Browse == "ff") {
//var cNodes = XMLDoc.getElementsByTagName("Msg");
//HoneyMoonTitle.innerHTML = XMLDoc.getElementsByTagName("Msg")[0].childNodes[1].nodeValue;
var node = XMLDoc.getElementsByTagName("Msg");
HoneyMoonTitle.innerHTML = XMLDoc.getElementsByTagName("Msg")[0].childNodes[1].textContent;
}
else if (Browse == "ie") {
nodes = XMLDoc.documentElement.childNodes;
HoneyMoonTitle.innerText = nodes.item(0).childNodes.item(0).text;
myImages.src = ".." + nodes.item(0).childNodes.item(2).text;
myImages.alt = nodes.item(0).childNodes.item(0).text;
titleurl.href = nodes.item(0).childNodes.item(1).text;
imageurl.href = nodes.item(0).childNodes.item(1).text;
HoneyMoonList.innerHTML = nodes.item(0).childNodes.item(3).text;
}
}
</script>
XML测试文件:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Msg>
<Title>美丽的草原我的家</Title>
<URL>http://www.abc.com</URL>
<ImagePath>/Travel/Uploads/20111104150354_578.jpg</ImagePath>
<Contents>111111111111111</Contents>
</Msg>
</root>
请高手帮忙写个示例,非常感谢!
5 个解决方案
#1
首先 HoneyMoonTitle没定义 。
#2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>csdn</title>
<script type="text/javascript">
window.onload = function () {
HoneyMoon();
}
function HoneyMoon() {
var XMLDoc;
var Browse = "";
var XMLFile = "xml/test.xml"
if (window.ActiveXObject) {
XMLDoc = new ActiveXObject('Msxml2.DOMDocument');
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ie";
}else if (document.implementation && document.implementation.createDocument) {
XMLDoc = document.implementation.createDocument("", "", null);
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ff";
} else {
alert('您所使用的浏览器不兼容部分JavaScript功能!请使用IE、Firefox、Opera等浏览器。');
}
//根据浏览器不同执行不同显示
if (Browse == "ff") {
//var cNodes = XMLDoc.getElementsByTagName("Msg");
//HoneyMoonTitle.innerHTML = XMLDoc.getElementsByTagName("Msg")[0].childNodes[1].nodeValue;
var HoneyMoonTitle = document.getElementById("HoneyMoonTitle");
var node = XMLDoc.getElementsByTagName("Msg");
HoneyMoonTitle.innerHTML = node[0].childNodes[1].textContent;
}else if (Browse == "ie") {
var HoneyMoonTitle = document.getElementById("HoneyMoonTitle");
nodes = XMLDoc.documentElement.childNodes;
HoneyMoonTitle.innerText = nodes.item(0).childNodes.item(0).text;
myImages.src = ".." + nodes.item(0).childNodes.item(2).text;
myImages.alt = nodes.item(0).childNodes.item(0).text;
titleurl.href = nodes.item(0).childNodes.item(1).text;
imageurl.href = nodes.item(0).childNodes.item(1).text;
HoneyMoonList.innerHTML = nodes.item(0).childNodes.item(3).text;
}
}
</script>
</head>
<body>
<div id="HoneyMoonTitle"></div>
test
</body>
</html>
#3
我使用过的,你看一下
//读取xml文件内容
function loadXML(fileRoute){
var xmlDoc = null;
if (window.ActiveXObject){
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async = false;
xmlDoc.load(fileRoute);
} else if (document.implementation && document.implementation.createDocument){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",fileRoute,false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML.documentElement;
} else {
alert('您的浏览器不支持xml文件读取!');
return;
}
return xmlDoc;
}
#4
HoneyMoonTitle 是在HTML代码里的 我又没帖出来。
#5
在谷歌浏览器下还是有问题的,我改用Jquery ajax了
#1
首先 HoneyMoonTitle没定义 。
#2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>csdn</title>
<script type="text/javascript">
window.onload = function () {
HoneyMoon();
}
function HoneyMoon() {
var XMLDoc;
var Browse = "";
var XMLFile = "xml/test.xml"
if (window.ActiveXObject) {
XMLDoc = new ActiveXObject('Msxml2.DOMDocument');
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ie";
}else if (document.implementation && document.implementation.createDocument) {
XMLDoc = document.implementation.createDocument("", "", null);
XMLDoc.async = false;
XMLDoc.load(XMLFile);
Browse = "ff";
} else {
alert('您所使用的浏览器不兼容部分JavaScript功能!请使用IE、Firefox、Opera等浏览器。');
}
//根据浏览器不同执行不同显示
if (Browse == "ff") {
//var cNodes = XMLDoc.getElementsByTagName("Msg");
//HoneyMoonTitle.innerHTML = XMLDoc.getElementsByTagName("Msg")[0].childNodes[1].nodeValue;
var HoneyMoonTitle = document.getElementById("HoneyMoonTitle");
var node = XMLDoc.getElementsByTagName("Msg");
HoneyMoonTitle.innerHTML = node[0].childNodes[1].textContent;
}else if (Browse == "ie") {
var HoneyMoonTitle = document.getElementById("HoneyMoonTitle");
nodes = XMLDoc.documentElement.childNodes;
HoneyMoonTitle.innerText = nodes.item(0).childNodes.item(0).text;
myImages.src = ".." + nodes.item(0).childNodes.item(2).text;
myImages.alt = nodes.item(0).childNodes.item(0).text;
titleurl.href = nodes.item(0).childNodes.item(1).text;
imageurl.href = nodes.item(0).childNodes.item(1).text;
HoneyMoonList.innerHTML = nodes.item(0).childNodes.item(3).text;
}
}
</script>
</head>
<body>
<div id="HoneyMoonTitle"></div>
test
</body>
</html>
#3
我使用过的,你看一下
//读取xml文件内容
function loadXML(fileRoute){
var xmlDoc = null;
if (window.ActiveXObject){
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async = false;
xmlDoc.load(fileRoute);
} else if (document.implementation && document.implementation.createDocument){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",fileRoute,false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML.documentElement;
} else {
alert('您的浏览器不支持xml文件读取!');
return;
}
return xmlDoc;
}
#4
HoneyMoonTitle 是在HTML代码里的 我又没帖出来。
#5
在谷歌浏览器下还是有问题的,我改用Jquery ajax了