XML DOM data属性
定義と使用方法
data
属性を設定またはコメントのテキストを返します。
文法
commentNode.data
例
以下のコードは、"books_comment.xml"をxmlDocにロードし、最初の<book>要素のコメントテキストを出力します:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books_comment.xml", true); xhttp.send(); function myFunction(xml) { var x, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book")[0].childNodes; for (i = 0; i < x.length; i++) { if (x[i].nodeType == 8) { txt += x[i].data + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
上記の例では、コメントノードのみを処理するようにループと if テストを使用しました。コメントノードのノードタイプは 8 です。