您的位置:首页 > 其它

跨浏览器解析XML字符串(三)

2011-07-27 13:31 357 查看
<!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>解析XML字符串</title>
</head>
<body>
<script language="javascript" type="text/javascript">
//创建XML字符串
var str = "<唐诗>";
str += "<Item id='01'>";
str += "<诗名>静夜思</诗名>";
str += "<作者>李白</作者>";
str += "<内容>床前明月光,疑是地上霜。举头望明月,低头思故乡。</内容>";
str += "</Item>";
str += "</唐诗>";

var xmldoc=null;
if(window.ActiveXObject){ //判断是否为IE浏览器
xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async=false;
xmldoc.loadXML(str);
}else{
try{ //其他浏览器
var domParser = new DOMParser();
xmldoc = domParser.parseFromString(str,"text/xml");
}
catch(ex)
{
}
}

if( xmldoc != null) //判断是否已经创建XMLDOM
{
document.write("该xml文件的根元素名称为:"+xmldoc.documentElement.nodeName);

}else{
alert("您的浏览器不支持XML解析器");
}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: