XML DOM သည် Node တန်ဖိုးများကို ရယူပါ။
nodeValue ပိုင်ဆိုင်မှုကို node တစ်ခု၏စာသားတန်ဖိုးကိုရယူရန်အသုံးပြုသည်။
getAttribute() method သည် attribute တစ်ခု၏တန်ဖိုးကို ပြန်ပေးသည်။
Element တစ်ခု၏တန်ဖိုးကိုရယူပါ။
DOM တွင်၊ အရာအားလုံးသည် node တစ်ခုဖြစ်သည်။ Element node များတွင် စာသားတန်ဖိုးမရှိပါ။
element node တစ်ခု၏ စာသားတန်ဖိုးကို ကလေး node တွင် သိမ်းဆည်းထားသည်။ ဤ node ကို text node ဟုခေါ်သည်။
ဒြပ်စင်တစ်ခု၏ စာသားတန်ဖိုးကို ပြန်လည်ရယူရန်၊ သင်သည် ဒြပ်စင်များ၏ စာသားအမှတ်အသားတန်ဖိုးကို ပြန်လည်ရယူရပါမည်။
getElementsByTagName နည်းလမ်း
getElementsByTagName () နည်းလမ်းသည် အရင်းအမြစ်စာရွက်စာတမ်းတွင် ၎င်းတို့ပေါ်လာသည်နှင့် တူညီသောအစီအစဥ်ဖြင့် သတ်မှတ်ထားသော တဂ်အမည်ဖြင့် အစိတ်အပိုင်းအားလုံး၏ node စာရင်းကို ပြန်ပေးသည်။
" books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့။
ဤကုဒ်သည် ပထမဆုံး <title> ဒြပ်စင်ကို ရယူသည်-
var x = xmlDoc.getElementsByTagName("title")[0];
ChildNodes ပိုင်ဆိုင်မှု
childNodes ပိုင်ဆိုင်မှုသည် ဒြပ်စင်တစ်ခု၏ ကလေး node များစာရင်းကို ပြန်ပေး သည် ။
အောက်ပါကုဒ်သည် ပထမ <title> ဒြပ်စင်၏ စာသား Node ကို ထုတ်ယူသည်-
x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
nodeValue ပိုင်ဆိုင်မှု
nodeValue ပိုင်ဆိုင်မှုသည် text node တစ်ခု၏ စာသားတန်ဖိုးကို ပြန်ပေး သည် ။
အောက်ပါကုဒ်သည် ပထမ <title> ဒြပ်စင်၏ text node ၏ စာသားတန်ဖိုးကို ရယူသည်-
ဥပမာ
x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
z = y.nodeValue;
z ရလဒ်- "နေ့တိုင်း အီတလီ"
ပြည့်စုံသော ဥပမာ
ဥပမာ
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
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 xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName('title')[0];
var y =
x.childNodes[0];
document.getElementById("demo").innerHTML = y.nodeValue;
}
</script>
</body>
</html>
<title> ဒြပ်စင်များအားလုံးကို လှည့်ပတ်ကြည့်ပါ-
Attribute တစ်ခု၏တန်ဖိုးကိုရယူပါ။
DOM တွင်၊ attribute များသည် node များဖြစ်သည်။ ဒြပ်စင် Node များနှင့်မတူဘဲ၊ attribute nodes တွင် စာသားတန်ဖိုးများရှိသည်။
attribute တစ်ခု၏တန်ဖိုးကိုရရန်နည်းလမ်းမှာ ၎င်း၏စာသားတန်ဖိုးကိုရယူရန်ဖြစ်သည်။
getAttribute() method ကို အသုံးပြု၍ သို့မဟုတ် attribute node ၏ nodeValue ပိုင်ဆိုင်မှုကို အသုံးပြု၍ လုပ်ဆောင်နိုင်သည်။
Attribute တန်ဖိုးတစ်ခုရယူပါ - getAttribute()
getAttribute () method သည် attribute တစ်ခု၏တန်ဖိုးကို ပြန်ပေးသည် ။
အောက်ပါကုဒ်သည် ပထမ <title> ဒြပ်စင်၏ "lang" ရည်ညွှန်းချက်၏ စာသားတန်ဖိုးကို ထုတ်ယူသည်-
ဥပမာ
x = xmlDoc.getElementsByTagName("title")[0];
txt = x.getAttribute("lang");
txt ရလဒ်- "en"
<book> ဒြပ်စင်များအားလုံးကို လှည့်ပတ်ပြီး ၎င်းတို့၏ "အမျိုးအစား" ရည်ညွှန်းချက်များကို
Attribute တန်ဖိုးတစ်ခုရယူပါ - getAttributeNode()
getAttributeNode () method သည် attribute node ကို ပြန်ပေးသည် ။
အောက်ပါကုဒ်သည် ပထမ <title> ဒြပ်စင်၏ "lang" ရည်ညွှန်းချက်၏ စာသားတန်ဖိုးကို ထုတ်ယူသည်-
ဥပမာ
x = xmlDoc.getElementsByTagName("title")[0];
y = x.getAttributeNode("lang");
txt = y.nodeValue;
txt = "en" ရလဒ်
<book> ဒြပ်စင်များအားလုံးကို လှည့်ပတ်ပြီး ၎င်းတို့၏ "အမျိုးအစား" ရည်ညွှန်းချက်များကို ရယူပါ-