0

私はajaxでリッピングしているxmlファイルを持っています:

<country name="UK">
        <person>
            <name>daniel</name>
            <age>25</city>
        </person>
        <person>
            <name>james</name>
            <age>22</city>
        </person>
        <person>
            <name>brandy</name>
            <age>16</city>
        </person>
        <person>
            <name>nathan</name>
            <age>66</city>
        </person>
    </country>
    <country name="france">
        <person>
            <name>paul</name>
            <age>28</city>
        </person>
        <person>
            <name>pierr</name>
            <age>22</city>
        </person>
    </country>

私のJSは次のようになります:

<script type="text/javascript">
function loadXMLDoc() {
xmlhttp=null;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() 
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          xmlDoc=xmlhttp.responseXML;
          var txt="";
          x=xmlDoc.getElementsByTagName("name");
          for (i=0;i<x.length;i++)
            {
            txt=txt + x[i].childNodes[0].nodeValue + "<br />";
            }
          document.getElementById("myDivName").innerHTML=txt;
          //somehow get the country of that person to write here
          document.getElementById("myDivCountry").innerHTML=txt;

          }
      xmlhttp.open("GET","people.xml",true);
      xmlhttp.send();
      }

}
</script>

JS は人の名前を出力しますが、その人が属する国の名前を取得するにはどうすればよいですか?

これは、この問題のために書かれたすべてのサンプル コンテンツです... XML を修正しませんでした。これは、フィルタリングしたい別の場所からのフィードです。

ありがとう

ダン

4

1 に答える 1

0

プロパティを使用してタグparentNodeにアクセスcountryし、その属性名nameを使用してその値を取得できます。

var countryNames = "";
for (var i = 0; i < x.length; i++)
{
    txt += x[i].childNodes[0].nodeValue + "<br />";
    countryNames += x[i].parentNode.parentNode.name + "<br />";
}
于 2012-06-12T21:27:04.730 に答える