<input id="file01" type="file" onchange="show01(this)"/>
function initialxmlDoc()
{
//將xmlstring轉成xml物件 xmlDoc
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(xmlstring,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlstring);
}
pro=xmlDoc.getElementsByTagName("product");
}
}
步驟1:建立next()函數來控制到下一筆
function next()
{
if(pro == undefined){initialxmlDoc();}
i++;
if(i>pro.length){i=0;}
changedata(i);
}
步驟2:建立previous()函數來控制到下一筆
function previous()
{
if(pro == undefined){initialxmlDoc();}
i--;
if(i<0){i=pro.length-1;}
changedata(i);
}
步驟3:建立顯示單筆資料的changedata(i)
var i=0;
var pro;
function changedata(i)
{
str ="名稱:";
str += pro[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
str +="<br/>價錢:";
str +=pro[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
str +="<br/>庫存:";
str +=pro[i].getElementsByTagName("qty")[0].childNodes[0].nodeValue;
document.getElementById("id01").innerHTML = str;
}
//將字串轉成xmlDoc節點變數 |
//將xml物件轉成string文字 |
<?xml version="1.0" standalone="yes"?>
<csie>
<product>
<name>阿Q桶麵</name>
<price>30</price>
<qty>30</qty>
</product>
<product>
<name>可口可樂</name>
<price>15</price>
<qty>15</qty>
</product>
<product>
<name>義美水餃</name>
<price>85</price>
<qty>50</qty>
</product>
<product>
<name>鐵路便當</name>
<price>60</price>
<qty>10</qty>
</product>
</csie>