English 中文(简体)
阅读XML内容,并使用Java语进入超文本页面
原标题:Read XML contents and put into HTML page using JavaScript

我需要通过XML文件阅读,该档案中含有Info,但一个时段,并用超文本显示。 它需要像在“密码”和“设计”;div>内,以<代码>和t;p>的形式出现。 有些人能够帮助我如何做到这一点?

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>

Java:

<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>
最佳回答

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.com/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.
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签