当前位置: 代码迷 >> JavaScript >> JS解析XML资料和XML字符串
  详细解决方案

JS解析XML资料和XML字符串

热度:626   发布时间:2012-09-20 09:36:50.0
JS解析XML文件和XML字符串

JS 解析 XML 文件

< scrip t?type = ' text/javascript ' >
????loadXML?
= ? function (xmlFile){
????????
var ?xmlDoc = null ;
????????
// 判断浏览器 的类型
???????? // 支持IE浏览器
???????? if ( ! win dow.DOMParser? && ?window.ActiveXObject){
????????????
var ?xmlDomVersions? = ?[ ' MSXML.2.DOMDocument.6.0 ' , ' MSXML.2.DOMDocument.3.0 ' , ' Microsoft.XMLDOM ' ];
????????????
for ( var ?i = 0 ;i < xmlDomVersions.length;i ++ ){
????????????????
try {
????????????????????xmlDoc?
= ? new ?ActiveXObject(xmlDomVersions[i]);
????????????????????
break ;
????????????????}
catch (e){
????????????????}
????????????}
????????}
????????
// 支持Mozilla浏览器
???????? else ? if (document.implementation? && ?document.implementation.createDocument){
????????????
try {
????????????????
/* ?document.implementation.createDocument('','',null);?方法 的三个参数说明
?????????????????*?第一个参数是包含文档所使用的命名空间 URI的字符串;?
?????????????????*?第二个参数是包含文档根元素名称的字符串;?
?????????????????*?第三个参数是要创建的文档类型(也称为doctype)
?????????????????
*/
????????????????xmlDoc?
= ?document.implementation.createDocument( '' , '' , null );
????????????}
catch (e){
????????????}
????????}
????????
else {
????????????
return ? null ;
????????}

????????
if (xmlDoc != null ){
????????????xmlDoc.async?
= ? false ;
????????????xmlDoc.load(xmlFile);
????????}
????????
return ?xmlDoc;
????}
< / script>

?

?

JS解析XML字符串

?

< script?type = ' text/javascript ' >
????loadXML?
= ? function (xmlString){
????????
var ?xmlDoc = null ;
????????
// 判断浏览器的类型
???????? // 支持IE浏览器?
???????? if ( ! window.DOMParser? && ?window.ActiveXObject){??? // window.DOMParser?判断是否是非ie浏览器
???????????? var ?xmlDomVersions? = ?[ ' MSXML.2.DOMDocument.6.0 ' , ' MSXML.2.DOMDocument.3.0 ' , ' Microsoft.XMLDOM ' ];
????????????
for ( var ?i = 0 ;i < xmlDomVersions.length;i ++ ){
????????????????
try {
????????????????????xmlDoc?
= ? new ?ActiveXObject(xmlDomVersions[i]);
????????????????????xmlDoc.async?
= ? false ;
????????????????????xmlDoc.loadXML(xmlString);?
// loadXML方法载入xml字符串
???????????????????? break ;
????????????????}
catch (e){
????????????????}
????????????}
????????}
????????
// 支持Mozilla浏览器
???????? else ? if (window.DOMParser? && ?document.implementation? && ?document.implementation.createDocument){
????????????
try {
????????????????
/* ?DOMParser?对象解析?XML?文本并返回一个?XML?Document?对象。
?????????????????*?要使用?DOMParser,使用不带参数的构造 函数来实例化它,然后调用其?parseFromString()?方法
?????????????????*?parseFromString(text,?contentType)?参数text:要解析的?XML?标记?参数contentType文本的内容 类型
?????????????????*?可能是?"text/xml"?、"application/xml"?或?"application/xhtml +xml"?中的一个。注意 ,不支持?"text/html "。
?????????????????
*/
????????????????domParser?
= ? new ??DOMParser();
????????????????xmlDoc?
= ?domParser.parseFromString(xmlString,? ' text/xml ' );
????????????}
catch (e){
????????????}
????????}
????????
else {
????????????
return ? null ;
????????}

????????
return ?xmlDoc;
????}
< / script>

?

测试XML

?

<? xml?version="1.0"?encoding="utf-8"? ?>
< DongFang >
??
< Company >
????
< cNname > 1 </ cNname >
????
< cIP > 1 </ cIP >
??
</ Company >
??
< Company >
????
< cNname > 2 </ cNname >
????
< cIP > 2 </ cIP >
??
</ Company > ????
??
< Company >
????
< cNname > 3 </ cNname >
????
< cIP > 3 </ cIP >
??
</ Company >
??
< Company >
????
< cNname > 4 </ cNname >
????
< cIP > 4 </ cIP >
??
</ Company >
??
< Company >
????
< cNname > 5 </ cNname >
????
< cIP > 5 </ cIP >
??
</ Company >
??
< Company >
????
< cNname > 6 </ cNname >
????
< cIP > 6 </ cIP >
??
</ Company >
</ DongFang >

?

使用方法

?var xmldoc=loadXML(text.xml)

?var elements = xmlDoc.getElementsByTagName("Company ");

?for (var i = 0; i < elements.length; i++) {
??????????????? var id = elements[i].getElementsByTagName("cNname ")[0].firstChild.nodeValue;
??????????????? var name = elements[i].getElementsByTagName("cIP ")[0].firstChild.nodeValue;???????????????

}
上一篇:? Custom Sharepoint 2010 Menu
下一篇:? 2011第一季度国内移动互联网收入190亿

?

  相关解决方案