<input id="file01" type="file" onchange="show01(this)"/>
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);
}
//將xmlDoc輸出成表格資料
str ="<table border=1><tr><td>名稱</td><td>價錢</td><td>庫存</td></tr>";
var pro=xmlDoc.getElementsByTagName("product");
for (i=0;i<pro.length;i++)
{
str +="<tr><td>";
str +=pro[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
str +="</td><td>";
str +=pro[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
str +="</td><td>";
str +=pro[i].getElementsByTagName("qty")[0].childNodes[0].nodeValue;
str +="</td></tr>";
}
str +="</table>";
document.getElementById("id01").innerHTML = str;
<table border=1>
<tr>
<td>學號</td><td>姓名</td><td>電話</td><td>電子郵件</td>
</tr>
//將字串轉成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>