码迷,mamicode.com
首页 > 其他好文 > 详细

解析XML字符串,跨浏览器实例

时间:2015-03-04 21:06:16      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

<!--解析XML字符串,跨浏览器实例-->
<html>
<head>
<script type = "text/javascript">
function parseXML()
{
text = "<note>";
text = "<to>George</to>";
text = "<from>Jhon</from>";
text = text + "<heading>Reminder</heading>";
text = text + "<body>Don‘t forget</body>";
text = text + "</note>";
try
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
}
catch(e)
{
try
{
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/html");
}
catch(e)
{
alter(e.message);
return;
}


}


document.getElementById("to").innerHTML = xmlDoc/getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML = xmlDoc.getELementsByTagName("from")[0].nodeNodes[0].nodeValue;
document.getElementById("message").innerHTML = xmlDoc.getElementsByTagName("Body")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload = "parseXML">
<h1>W3School.com.cn Internal Note</h1>
<p>
<b>To:</b> <span id = "to"></span><br />
<b>From:</b> <span id = "from"></span><br />
<b>Message:</b> <span id = "message"></span>
</p>
</body>
</html>

解析XML字符串,跨浏览器实例

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/44065171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!