跨浏览器解析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>
<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>
相关文章推荐
- js解析一个XML字符串 - 跨浏览器的实例
- 解析XML字符串,跨浏览器实例
- 解析 XML 字符串 - 一个跨浏览器的实例
- JS解析XML文件和字符串的跨浏览器实现
- JDOM解析XML字符串(非XML文档)
- pongo庞果:xml字符串文件的解析——为什么我的代码效率低?
- Android解析Xml字符串例子
- php遍历解析xml字符串的方法
- 解析ajax核心XMLHTTPRequest对象的创建与浏览器的兼容问题
- DOM解析XML字符串
- dom4j解析XML字符串
- java使用jdom架包解析xml字符串dom原理并保存map对象
- Javascript解析xml字符串,兼容firefox和ie
- js 解析xml文件和xml字符串
- Oracle中通过Function,存储过程,触发器,调用实现解析Clob字段中存在的xml字符串
- dom4j解析xml字符串实例-01
- js跨浏览器实现将字符串转化为xml对象的方法
- demo4j生成字符串xml和解析xml
- php解析xml字符串
- 关于XML字符串中出现非法字符导致解析出错的问题