Home  >  Article  >  Backend Development  >  Sample code sharing for JS reading XML data

Sample code sharing for JS reading XML data

黄舟
黄舟Original
2017-03-23 16:30:271617browse

Recently, due to consideration of access performance issues in the project, the inner pages have been generated static, but some of the content needs to be dynamic. According to the previous method, it should be Use JS to call a dynamic script file. I looked at Red Boy at the front end and it seems that some of the codes in it are quite good. They are all JS calling XML data. XML can be used directly. Dynamic script program to generate, haha, it’s a good thing. I simply studied it and just took it and used it.

The XMl data format is as follows:

<?xml version="1.0" encoding="gb2312"?>
<root>
<item>
<name>刘亦菲</name>
<url>MingXing/LiuYiFei.htm</url>
<color>7A9D4B</color>
</item>
<item>
<name>蔡依林</name>
<url>MingXing/CaiYiLin.htm</url>
<color>FD0000</color>
</item>
<item>
<name>张娜拉</name>
<url>MingXing/ZhangNaLa.htm</url>
<color>7A9D4B</color>
</item>
<item>
<name>张韶涵</name>
<url>MingXiang/ZhangShaoHan.htm</url>
<color>0000FF</color>
</item>
<item>
<name>张靓颖</name>
<url>MingXing/ZhangLiangYin.htm</url>
<color>7A9D4B</color>
</item>
<item>
<name>李宇春</name>
<url>MingXing/LiYuChun.htm</url>
<color>7A9D4B</color>
</item>
<item>
<name>徐若瑄</name>
<url>MingXing/XuLuXuan.htm</url>
<color>FD0000</color>
</item>
</root>

The front-end JS script code is as follows:
//Get the website’s popular click rankings

var cdsales=new ActiveXObject("Microsoft.XMLDOM"); //创建XmlDom对象
cdsales.async=true; //使用异步加载
cdsales.onreadystatechange=LoadedSales;
function LoadedSales()
{
    var txt="";
    if(cdsales.readyState==4)
    {
        if(cdsales.parseError.errorCode != 0) 
        {
            txt="";
        }else{
            var bi=cdsales.documentElement.selectNodes("item");
            if(bi!=null&&bi.length>0)
            {
                for(var i=0;i<bi.length;i++)
                {    
                    txt+="<li>·<a href="+bi[i].childNodes[1].text+" style=color:"+bi[i].childNodes[2].text+">"+bi[i].childNodes[0].text+"</a></li>";
                }
            }else{
                txt="";
            }
        }
    }else{
        txt="";
    }    
    sales.innerHTML=txt;
}
function LoadSalesDoc()
{
    var Url="/XML/Hot.xml";
    cdsales.load(Url);
}

The above Hot.Xml can be accessed using the program Automatically generated, as long as the output page is in Xml format. It seems that more and more websites have begun to use p+JS+XML to architecture. Haha, it is quite good to learn the code first. At least static pages can also be read dynamically. Database, I don’t know if this is the concept of Ajax, it should be regarded as it.

The above is the detailed content of Sample code sharing for JS reading XML data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn