最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Read XML contents and put into HTML page using JavaScript - Stack Overflow

programmeradmin0浏览0评论

I need to read through the XML file that contains info but one at a time and display in an HTML page. It needs to appear like its scrolling on its own within the HTML inside a <div> and in form of <p>. Can some one help me out with how to do this?

abc.xml

<?xml version="1.0" encoding="iso-8859-1" ?>

<projects>

<project>
        <flag>1</flag>
        <name>Project 1</name>
        <descp> Short description. </descp>
        <rating> 6 </rating>
        <link> URL1 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 2</name>
        <descp> Short description. </descp>
        <rating> 9 </rating>
        <link> URL2 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 3</name>
        <descp> Short description. </descp>
        <rating> 4 </rating>
        <link> URL3 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 4</name>
        <descp> Short description. </descp>
        <rating> 5 </rating>
        <link> URL4 </link>
        </project>

</projects>

JavaScript:

<script type="text/javascript">
window.onload = timer;

function timer() {
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
    xmlDoc.async="false";
    xmlDoc.load("./gsss_hp.xml");
    var projects = xmlDoc.documentElement;
    var project = projects.childNodes(0);
    var numPro = projects.childNodes.length;   // to find number of projects entered in the file

    for(var i=0; i<numPro; i++){
        for(var j=0; j<100000;j++){j+=18; j-=18; for(var z=0;z<5000;z++){}}  /* do noting for nested loops */
        show(i);
    }
}

function show(num) {
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
    xmlDoc.async="false";
    xmlDoc.load("./gsss_hp.xml");

    var projects = xmlDoc.documentElement;
    var project = projects.childNodes(num);
    var attr = project.childNodes.length;

    for(var i=0;i<attr;i++){
        var txt = xmlDoc.getElementsByTagName("project")[0].childNodes[i].nodeValue;
        txt = txt + "<br />";
        var idPos;
        if(i==1) { idPos = "name"; }
        else
        if(i==2) { idPos = "descp"; }
        else
        if(i==3) { idPos = "rating"; }
        else
        if(i==4) { idPos = "link"; }

        document.getElementById(idPos).innerHTML = txt;
    }
}

</script>

I need to read through the XML file that contains info but one at a time and display in an HTML page. It needs to appear like its scrolling on its own within the HTML inside a <div> and in form of <p>. Can some one help me out with how to do this?

abc.xml

<?xml version="1.0" encoding="iso-8859-1" ?>

<projects>

<project>
        <flag>1</flag>
        <name>Project 1</name>
        <descp> Short description. </descp>
        <rating> 6 </rating>
        <link> URL1 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 2</name>
        <descp> Short description. </descp>
        <rating> 9 </rating>
        <link> URL2 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 3</name>
        <descp> Short description. </descp>
        <rating> 4 </rating>
        <link> URL3 </link>
        </project>

<project>
        <flag>1</flag>
        <name>Project 4</name>
        <descp> Short description. </descp>
        <rating> 5 </rating>
        <link> URL4 </link>
        </project>

</projects>

JavaScript:

<script type="text/javascript">
window.onload = timer;

function timer() {
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
    xmlDoc.async="false";
    xmlDoc.load("./gsss_hp.xml");
    var projects = xmlDoc.documentElement;
    var project = projects.childNodes(0);
    var numPro = projects.childNodes.length;   // to find number of projects entered in the file

    for(var i=0; i<numPro; i++){
        for(var j=0; j<100000;j++){j+=18; j-=18; for(var z=0;z<5000;z++){}}  /* do noting for nested loops */
        show(i);
    }
}

function show(num) {
    var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
    xmlDoc.async="false";
    xmlDoc.load("./gsss_hp.xml");

    var projects = xmlDoc.documentElement;
    var project = projects.childNodes(num);
    var attr = project.childNodes.length;

    for(var i=0;i<attr;i++){
        var txt = xmlDoc.getElementsByTagName("project")[0].childNodes[i].nodeValue;
        txt = txt + "<br />";
        var idPos;
        if(i==1) { idPos = "name"; }
        else
        if(i==2) { idPos = "descp"; }
        else
        if(i==3) { idPos = "rating"; }
        else
        if(i==4) { idPos = "link"; }

        document.getElementById(idPos).innerHTML = txt;
    }
}

</script>
Share Improve this question edited Jul 4, 2011 at 8:59 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Jul 4, 2011 at 8:53 BalaBala 3903 gold badges8 silver badges25 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 1

While I'll leave the actual coding to you, the general structure I would use for something like this is:

  • Start off by loading the XML.
  • When the XML is loaded, extract the data into a regular javascript array, to make it easier to handle.
  • Use setTimeout to call draw-function at a specified interval ( http://www.w3schools./js/js_timing.asp ) and set a (global) variable to remember which index is the last one displayed.
  • The draw function displays the data, and increments the variable. If == length, reset count.
发布评论

评论列表(0)

  1. 暂无评论