XML DOM 获取节点值

nodeValue 属性用于获取节点的文本值。

getAttribute() 方法返回属性的值。

获取元素的值

在 DOM 中,一切都是节点。元素节点没有文本值。

အရာဝတ္တု အချက်အလက် အချက်အလက် ကို အစားအထား အချက်အလက် အချက်အလက် တွင် သို့မဟုတ် အစားအထား အချက်အလက် အချက်အလက် တွင် သို့မဟုတ်

အရာဝတ္တု အချက်အလက် ရယူခြင်း အစား အစားအထား အချက်အလက် အချက်အလက် ကို ရယူခြင်း

ထို့ကြောင်း အရာဝတ္တု အချက်အလက် ရယူခြင်း မူကြောင်း အစား အစားအထား အချက်အလက် အချက်အလက် အချက်အလက် ကို ရယူခြင်း

getElementsByTagName အမှုဆောင်

getElementsByTagName() အမှုဆောင် အချက်အလက် ပေါ်ပေါက်လာသော အရာဝတ္တု အချက်အလက် စာရင်း ပြန်လည်ပေးသည်အရာဝတ္တု အချက်အလက် စာရင်း။ အရာဝတ္တု အမှတ်ပေးခြင်း အစား အရာဝတ္တု ပေါ်ပေါက်လာသော အမှတ်ပေးခြင်း အစား

ခန့်မှန်း books.xml ပြောင်းလဲသည်မှတစ်ဆင်း xmlDoc တွင်。

အချက်အလက် ပြောင်းလဲသည်: ပထမ <title> အစားအထား ပြောင်းလဲသည်:

var x = xmlDoc.getElementsByTagName("title")[0];

ChildNodes အချက်အလက်

childNodes အချက်အလက်အစားအထား အချက်အလက် စာရင်း

အောက်ပါ ကြောင်းရပ်များ ပထမ <title> အစားအထား အချက်အလက်:

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

nodeValue အချက်အလက်

nodeValue အချက်အလက်အချက်အလက် အချက်အလက်

အောက်ပါ ကြောင်းရပ်များ ပထမ <title> အစားအထား အချက်အလက် အချက်အလက် ကို ရယူကြသည်:

အကြောင်းကြား

x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
z = y.nodeValue;

z တွင် ရရှိသောအချက်: ရုပ်စာစက် သုံးစွဲ

ပုံစံအစား

ဘား 1

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xtpq = 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 xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName('title')[0];
    var y = x.childNodes[0];
}
document.getElementById("demo").innerHTML = y.nodeValue;
</script>
</body>

亲自试一试

ဘား 2

</html>

အားလုံး <title> အရာကို လည်ပတ်လေ့လာပါ:
for (i = 0; i < x.length; i++) { 
    x = xmlDoc.getElementsByTagName('title');
}

亲自试一试

txt += x[i].childNodes[0].nodeValue + "<br>";

အခွင့်အရေး အယူအယွင်း ကို ရယူပါတယ်。

ပုံစံအားလုံး တွင် အခွင့်အရေး သည် ပိုင်းခွဲ ဖြစ်သည်။ အခြား အရာကို သုံးပြီး အသုံးပြုသော အခွင့်အရေး ပိုင်းခွဲ နှင့် နှိုင်းယှဉ်လျှင် အခွင့်အရေး ပိုင်းခွဲ သည် အယူအယွင်း အရာ ရှိပါတယ်。

အခွင့်အရေး အယူအယွင်း ကို ရယူရန် ဘဒ္ဒန္တ သုံးပြီး: getAttribute() ဘဒ္ဒန္တ သို့မဟုတ် အခွင့်အရေး ပိုင်းခွဲ: nodeValue အခွင့်အရေး ကို လုပ်ကိုင်ရန် အသုံးပြုပါတယ်。

အခွင့်အရေး အယူအယွင်း - getAttribute()

getAttribute() getAttributeNode() ဘဒ္ဒန္တ ကို ကွပ်ကဲပါတယ်:အခွင့်အရေး အယူအယွင်း:

ပထမ <title> အရာကို ရှာဖွေသော ကြောင်းဖြင့်: "lang" အခွင့်အရေး အယူအယွင်း:

ဘား 1

x = xmlDoc.getElementsByTagName("title")[0];
txt = x.getAttribute("lang");

亲自试一试

ဘား 2

အားလုံး <book> အရာကို လည်ပတ်လေ့လာပြီး "category" အခွင့်အရေး ကို ရယူပါတယ်:

x = xmlDoc.getElementsByTagName("book");
for (i = 0; i < x.length; i++) { 
    txt += x[i].getAttribute("category") + "<br>";
}

亲自试一试

အခွင့်အရေး အယူအယွင်း - getAttributeNode()

getAttributeNode() getAttributeNode() ဘဒ္ဒန္တ ကို ကွပ်ကဲပါတယ်:အခွင့်အရေး ပိုင်းခွဲ:

ပထမ <title> အရာကို ရှာဖွေသော ကြောင်းဖြင့်: "lang" အခွင့်အရေး အယူအယွင်း:

ဘား 1

x = xmlDoc.getElementsByTagName("title")[0];
y = x.getAttributeNode("lang");
txt = y.nodeValue;

亲自试一试

ဘား 2

အားလုံး <book> အရာကို လည်ပတ်လေ့လာပြီး "category" အခွင့်အရေး ကို ရယူပါတယ်:

x = xmlDoc.getElementsByTagName("book");
for (i = 0; i < x.length; i++) {
    txt += x[i].getAttributeNode("category").nodeValue + "
"; }

亲自试一试