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:271485browse

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:




刘亦菲
MingXing/LiuYiFei.htm
7A9D4B


蔡依林
MingXing/CaiYiLin.htm
FD0000


张娜拉
MingXing/ZhangNaLa.htm
7A9D4B


张韶涵
MingXiang/ZhangShaoHan.htm
0000FF


张靓颖
MingXing/ZhangLiangYin.htm
7A9D4B


李宇春
MingXing/LiYuChun.htm
7A9D4B


徐若瑄
MingXing/XuLuXuan.htm
FD0000

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[i].childNodes[0].text+"";
                }
            }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