JS操作XML实例

来源:互联网 发布:java的https通信 编辑:程序博客网 时间:2024/05/29 16:33
<!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=gb2312" />    
        <title>JS读取XML兼容IE/FF/Chrome,Safari - www.cxybl.com</title>    
        <style>
        li{list-style:none;}    
        </style>
        <script type="text/javascript"> 
        var orderDoc;    
        //获取xml文件    
        function loadXmlFile(xmlFile){    
          var xmlDom = null;    
          if (window.ActiveXObject){    
            xmlDom = new ActiveXObject("Microsoft.XMLDOM");    
            xmlDom.async=false;    
           xmlDom.load(xmlFile);//如果用的是XML字符串//如果用的是xml文件xmlDom.loadXML(xmlFile);
          }else if (document.implementation && document.implementation.createDocument){    
            var xmlhttp = new window.XMLHttpRequest();    
            xmlhttp.open("GET", xmlFile, false);    
            xmlhttp.send(null);    
            xmlDom = xmlhttp.responseXML;    
          }else{    
            xmlDom = null;    
          }  
          return xmlDom;    
        }    
        //判断子节点为不为空    
        function isnull(obj)    
        {    
            var nodevalue = "";    
            if(obj.childNodes[0] != null)    
            {    
                nodevalue =obj.childNodes[0].nodeValue;    
            }    
            return nodevalue;    
        }    
        var stringsss="";    
        //根据编号获取数据    
        function getDataByid(number)    
        {
            var file =isnull(orderDoc.getElementsByTagName("file")[number]);  
            var title =isnull(orderDoc.getElementsByTagName("title")[number]);    
            var artist =isnull(orderDoc.getElementsByTagName("artist")[number]);    
            var album = isnull(orderDoc.getElementsByTagName("album")[number]);   
            var std='<li>文件名:'+file+'<br />标题:'+title+'<br />艺术家'+artist+'<br/>专辑:'+album+'</li>';    
            return std;    
        }
        //获得页面内容    
        function getContent(){    
            //exBrows();    
            orderDoc=loadXmlFile("jsReadXml.xml"); 
                var nodes=orderDoc.documentElement.childNodes;
                var items=nodes.length;    
                //alert(items);    
                var htmlstr="";    
                stringsss+='<ul>';    
                for(i=0;i<items;i++){
                   for(j=0;j<nodes.item(i).childNodes.length;j++){
                    stringsss+=getDataByid(j);}  
                }    
                stringsss+='</ul>';                
                document.write(stringsss);    
                
        }
        </script>
        </head>
        <body>   
         <input type=submit value=dd onclick=getContent() />
        </body>    
        <script type="text/javascript" language="javascript">    
        
        </script>    

        </html>


XML内容

<?xml version="1.0" encoding="UTF-8" ?> 
<player>
<playlist>
    <track>
      <file>枫林标题.mp3</file>
      <title>枫林头mp3_月之门游戏背景音乐。秋日,洛阳效外萧瑟的枫林让人追思无限。此情此景,勾起往事,不禁暗自神伤。</title>
      <artist>枫林描述mp3</artist>
      <album>枫林mp3</album>     
    </track>    


    <track>
      <file>123.mp3</file>
      <title>音乐之王</title>
      <artist>我们</artist>
      <album>中国</album>
</track>    


<track>
      <file>4564564.mp3</file>
      <title>音乐之王1</title>
      <artist>我们2</artist>
      <album>中国3</album>
</track>    


</playlist>
</player>