2012-03-15 13 views
0

"weather"要素の子要素を出力する次のコードがあります。動的に読み込まれた要素の子を取得する

x=xmlDoc.getElementsByTagName("weather")[0].childNodes; 

    for (i=0;i<x.length;i++) { 
     var parent = x[i].nodeName; 
     document.write("<b>"+parent+"</b><br />"); 

    } 

これは、次のチャイルズれます:

forecast_information、current_conditions、forecast_conditions、 forecast_conditions、forecast_conditionsとforecast_conditionsを。

ここで、これらの要素のすべての子を取得します。おそらくループ内でループして、私は次のことを試した理由です:

for (i=0;i<x.length;i++) { 
    var parent = x[i].nodeName; 
    document.write("<b>"+parent+"</b><br />"); 

    y=xmlDoc.getElementsByTagName(parent)[i].childNodes; 

    for (h=0;h<y.length;h++) { 
     var child = y[i].nodeName; 
     document.write(child+"<br />"); 
    } 
} 

しかし、これは動作しません...それは出力を取得ウィル:

forecast_information

街を(7回)

current_conditions

ザッツすべて..どんな助け?

ありがとうございます!

答えて

0

getElementsByTagNameを別の呼び出しで使用しないでください。あなたのループ内の各要素にchildNodesを使用して、子供の子供を取得してください:

var x = xmlDoc.getElementsByTagName("weather")[0].childNodes; 
var xlen = x.length; 

var i, j, y, ylen; 
for (i = 0; i < xlen; i++) { 
    document.write("<b>" + x[i].nodeName + "</b><br />"); 

    y = x[i].childNodes; 
    ylen = y.length; 

    for(j = 0; j < ylen; j++) { 
     document.write(y[j].nodeName + "<br />"); 
    } 
} 
+0

ああありがとうございます! – dimitri010

+0

@ dimitri010あなたは大歓迎です:すべての変数もグローバルに宣言することを忘れないでください。 – Paulpro

関連する問題