XML DOM childNodes 属性

定義と使用法

childNodes 属性はドキュメントのサブノードを返します。

ヒント:NodeListのlength属性を使用して、ノードリスト内のノード数を確認してください。ノードリストの長さを取得すると、簡単にループして必要な値を抽出できます!

構文

documentObject.childNodes

以下のコードは "books.xml" を xmlDoc に読み込み、XML ドキュメントのサブノードを表示します:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.childNodes;
    for (i = 0; i < x.length; i++) {
        txt += "Nodename: " + x[i].nodeName +
        " (nodetype: " + x[i].nodeType + ")";
    }
    document.getElementById("demo").innerHTML = txt;
}

実際に試してみる