これは本当に簡単なことですが、私はそれを機能させるために数時間苦労してきました.
私はjavascriptでXMLファイルを読んでいますが、配列のインデックスを(手動で)変更した場合にのみ、2行目を読み取ったときにのみ、自分のノードで読み取ることができません。
これは、行を書き出すために使用する行です
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
私が言ったように、 ("name")[0] を ("name")[1] に変更すると、2行目が読み取られます。おそらくループを作成できる方法はありますか?使用の長さ?
これはコードの一部です。
document.write("<table border='1'>");
var x =xmlDoc.getElementsByTagName("products");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write("</td></tr>");
}
document.write("</table>");
そしてXMLファイル
<productlist>
<products>
<title>Kök</title>
<product><id>23</id><name>Bestick</name><price>45</price></product>
<product><id>47</id><name>Tallrikar</name><price>99</price></product>
<product><id>54</id><name>Glas</name><price>64</price></product>
<product><id>68</id><name>Koppar</name><price>125</price></product>
</products>
<products>
<title>Sängkläder</title>
<product><id>12</id><name>Lakan</name><price>89</price></product>
<product><id>43</id><name>Kudde</name><price>148</price></product>
<product><id>48</id><name>Täcke</name><price>345</price></product>
</products>
</productlist>
ありがとう。