XML DOM သည် Nodes ကိုဖန်တီးပါ။
သင်ကိုယ်တိုင်စမ်းကြည့်ပါ - ဥပမာများ
အောက်ပါဥပမာများသည် XML ဖိုင် books.xml ကို အသုံးပြုသည် ။
ဒီနမူနာမှာ element node အသစ်တစ်ခုဖန်တီးရန် createElement() ကိုအသုံးပြုပြီး node တစ်ခုသို့ addChild() ကို appendChild() ကိုအသုံးပြုပါသည်။
ဤဥပမာသည် attribute node အသစ်တစ်ခုဖန်တီးရန် createAttribute() ကိုအသုံးပြုပြီး ၎င်းကို element တစ်ခုသို့ထည့်သွင်းရန်အတွက် setAttributeNode() ကိုအသုံးပြုပါသည်။
ဤဥပမာသည် element တစ်ခုအတွက် attribute အသစ်တစ်ခုဖန်တီးရန် setAttribute() ကိုအသုံးပြုသည်။
ဤဥပမာသည် စာသား node အသစ်တစ်ခုဖန်တီးရန် createTextNode() ကိုအသုံးပြုကာ ၎င်းကိုဒြပ်စင်တစ်ခုသို့ထည့်ရန် appendChild() ကိုအသုံးပြုသည်။
ဤဥပမာသည် CDATA အပိုင်း node တစ်ခုဖန်တီးရန် createCDATAsection() ကိုအသုံးပြုပြီး ၎င်းကိုဒြပ်စင်တစ်ခုသို့ထည့်ရန် appendChild() ကိုအသုံးပြုသည်။
ဤဥပမာသည် မှတ်ချက် node တစ်ခုဖန်တီးရန် createComment() ကိုအသုံးပြုပြီး ၎င်းကိုဒြပ်စင်တစ်ခုသို့ထည့်ရန် appendChild() ကိုအသုံးပြုသည်။
Element Node အသစ်တစ်ခုဖန်တီးပါ။
createElement () method သည် element node အသစ်တစ်ခုကို ဖန်တီးသည်-
ဥပမာ
newElement = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့
- Element Node အသစ် <edition> ကိုဖန်တီးပါ။
- ပထမ <book> ဒြပ်စင်သို့ ဒြပ်စင်ကို ပေါင်းထည့်ပါ။
လှည့်ပတ်ပြီး <book> ဒြပ်စင်များအားလုံးသို့ အစိတ်အပိုင်းတစ်ခုကို ပေါင်းထည့်ပါ-
New Attribute Node တစ်ခုဖန်တီးပါ။
createAttribute () ကို attribute node အသစ်တစ်ခုဖန်တီးရန်အသုံးပြုသည်-
ဥပမာ
newAtt = xmlDoc.createAttribute("edition");
newAtt.nodeValue = "first";
xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့
- attribute node "edition" အသစ်တစ်ခုဖန်တီးပါ
- attribute node ၏တန်ဖိုးကို "first" ဟုသတ်မှတ်ပါ
- ပထမ <title> ဒြပ်စင်သို့ attribute node အသစ်ကို ထည့်ပါ။
<title> ဒြပ်စင်အားလုံးကို လှည့်ပတ်ပြီး attribute node အသစ်တစ်ခုကို ထည့်ပါ-
ရည်ညွှန်းချက်ရှိပြီးသားဖြစ်ပါက ၎င်းကို အသစ်ဖြင့် အစားထိုးမည်ဖြစ်သည်။
setAttribute() ကိုအသုံးပြု၍ Attribute တစ်ခုဖန်တီးပါ
setAttribute () method သည် attribute မရှိလျှင် attribute အသစ်တစ်ခုကို ဖန်တီးပေးသောကြောင့်၊ attribute အသစ်တစ်ခုဖန်တီးရန် ၎င်းကို အသုံးပြုနိုင်ပါသည်။
ဥပမာ
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့
- ပထမ <book> ဒြပ်စင်အတွက် attribute "edition" value ကို "first" သို့ သတ်မှတ်ပါ။
<title> ဒြပ်စင်များအားလုံးကို လှည့်ပတ်ပြီး ရည်ညွှန်းချက်အသစ်တစ်ခုကို ထည့်ပါ-
Text Node တစ်ခုဖန်တီးပါ။
createTextNode () နည်းလမ်းသည် စာသား node အသစ်တစ်ခုကို ဖန်တီးသည်-
ဥပမာ
newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့
- Element Node အသစ် <edition> ကိုဖန်တီးပါ။
- စာသား "ပထမ" ဖြင့် စာသား node အသစ်တစ်ခု ဖန်တီးပါ
- စာသား node အသစ်ကို element node တွင်ထည့်ပါ။
- ပထမ <book> ဒြပ်စင်သို့ ဒြပ်စင်အသစ်ကို ပေါင်းထည့်ပါ။
<book> ဒြပ်စင်များအားလုံးသို့ စာသား node တစ်ခုဖြင့် ဒြပ်စင်တစ်ခုထည့်ပါ-
CDATA အပိုင်း Node တစ်ခုဖန်တီးပါ။
createCDATASection () method သည် CDATA အပိုင်း node အသစ်တစ်ခုကို ဖန်တီးသည်။
ဥပမာ
newCDATA = xmlDoc.createCDATASection("Special Offer & Book Sale");
xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည်ဆိုပါစို့
- CDATA ကဏ္ဍ node အသစ်တစ်ခု ဖန်တီးပါ။
- CDATA node အသစ်ကို ပထမဆုံး <book> ဒြပ်စင်သို့ ပေါင်းထည့်ပါ။
<book> ဒြပ်စင်များအားလုံးသို့ CDATA အပိုင်းကို လှည့်ပတ်ပြီး ထည့်ပါ-
မှတ်ချက် Node တစ်ခုဖန်တီးပါ။
createComment () method သည် comment node အသစ်တစ်ခုကို ဖန်တီးပေးပါသည်။
ဥပမာ
newComment = xmlDoc.createComment("Revised March 2015");
xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
ဥပမာ ရှင်းပြထားသည်-
- " books.xml " ကို xmlDoc တွင် တင်ထားသည် ဆိုပါစို့
- မှတ်ချက် Node အသစ်တစ်ခု ဖန်တီးပါ။
- ပထမဆုံး <book> ဒြပ်စင်သို့ မှတ်ချက်အသစ်ကို ပေါင်းထည့်ပါ။
လှည့်ပတ်ပြီး <book> အစိတ်အပိုင်းများအားလုံးသို့ မှတ်ချက်တစ်ခုထည့်ပါ-