JSでHttpXMLRequestオブジェクトを使用して取得しようとしていますが、XMLファイルから名前と年齢をロードしてテーブルにスローする基本的なループがあります。各兄弟が同じ値を持っている場合、それは正常にロードされますが、要素の1つが兄弟から欠落している場合、それは単にスキップするのではなく停止します。ここにいくつかのコードがあります。
HTML-
(The rest of the XML parser and DTD, html, body etc is up here)
<script>
var x=xmlDoc.getElementsByTagName("person");
document.write("<table border='1'>");
for (i=0;i<x.length;i++)
{
var name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
var age = x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue;
document.write("<tr>");
document.write("<td>"+name+"</td>");
document.write("<td>"+age+"</td>");
document.write("</tr>");
}
document.write("</table>");
</script>
XML-
(The DTD and stuff it up here)
<people>
<person>
<name>Jim</name>
<age>20</age>
</person>
<person>
<name>Bob</name>
</person>
<person>
<name>Tom</name>
<age>20</age>
</person>
<person>
<name>James</name>
<age>20</age>
</person>
<person>
<name>Richie</name>
<age>20</age>
</person>
</people>
その例から、HTMLはbobになると書き込み後に停止します。名前と年齢ごとに、try / catchまたはifが必要だと思いますが、配置できないようで、機能します。誰かアイデアがありますか?
検索しようとしましたが、この問題が何と呼ばれているのかわかりません。また、それを説明しても何も起こりません。
単に「<age />
または<age></age>
」を追加することはオプションではありません。最終的には、大量のxmlファイルを処理することになります。存在しない場合に自動的に追加されるものを作成する必要がない限り、それは許容できると思います。
乾杯Hombries。