XML DOM కూడలు సృష్టించండి
కొత్త ఎలమెంట్ నోడ్ సృష్టించడం
createElement()
మార్గదర్శకం కొత్త ఎలమెంట్ నోడ్ సృష్టించడానికి విధము:
ఉదాహరణ 1
newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
ఉదాహరణ వివరణం:
- ఇక్కడ బుక్స్ ఎక్స్ఎల్ ప్రదర్శించబడింది అని భావించండి
xmlDoc
లో - ఒక కొత్త ఎలమెంట్ నోడ్ <edition> సృష్టించడం
- ఈ ఎలమెంట్ నోడ్ ను మొదటి <book> ఎలమెంట్కు జోడించడం
ఉదాహరణ 2
అన్ని <book> ఎలమెంట్లను చుట్టూ పరిశీలించి ఒక కొత్త ఎలమెంట్ను జోడించడానికి విధము:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("మొదటి ఎడిషన్"); newEle.appendChild(newText); x[i].appendChild(newEle); }
కొత్త అంశాన్ని సృష్టించడం
createAttribute()
కొత్త అంశాన్ని సృష్టించడానికి ఉపయోగించబడుతుంది:
ఉదాహరణ 1
newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "మొదటి ఎడిషన్"; xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
ఉదాహరణ వివరణం:
- ఇది books.xml లో లోడ్ అయ్యింది అని భావించవచ్చు:
xmlDoc
లో - కొత్త అంశాన్ని సృష్టించడం
"edition"
- ఈ అంశాన్ని విలువ సెట్ చేయబడింది:
"first"
- ఈ కొత్త అంశాన్ని మొదటి <title> ఎలమెంట్కు జోడించడం
ఉదాహరణ 2
అన్ని <title> ఎలమెంట్లను చుట్టూ పరిశీలించి కొత్త అంశాన్ని జోడించడానికి విధము:
for (i = 0; i < xLen; i++) { newAtt = xmlDoc.createAttribute("edition"); newAtt.value = "మొదటి ఎడిషన్"; x[i].setAttributeNode(newAtt); }
అంశం ఉన్నప్పుడు, దానిని కొత్త అంశంతో పునఃస్థాపిస్తారు.
setAttribute() ద్వారా అంశాన్ని సృష్టించడం
ఎందుకంటే: setAttribute()
మార్గదర్శకం అంశం లేకపోయినప్పుడు కొత్త అంశాన్ని సృష్టిస్తుంది, అందువల్ల కొత్త అంశాలను సృష్టించడానికి కూడా ఉపయోగించవచ్చు.
ఉదాహరణ 1
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
ఉదాహరణ వివరణం:
- ఇక్కడ బుక్స్ ఎక్స్ఎల్ ప్రదర్శించబడింది అని భావించండి
xmlDoc
లో - మొదటి <book> ఎలమెంట్ని యొక్క:
"edition"
అంశం విలువ సెట్ చేయబడింది:"first"
ఉదాహరణ 2
అన్ని <title> ఎలమెంట్లను చుట్టూ పరిశీలించి కొత్త అంశాన్ని జోడించడానికి విధము:
for(i = 0; i < x.length; i++) { x[i].setAttribute("edition", "మొదటి ఎడిషన్"); }
టెక్స్ట్ నోడ్ సృష్టించడం
createTextNode()
కథనం కొత్త టెక్స్ట్ నోడ్ సృష్టించడానికి విధము:
ఉదాహరణ 1
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("ప్రథమ"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
ఉదాహరణ వివరణం:
- ఇక్కడ బుక్స్ ఎక్స్ఎల్ ప్రదర్శించబడింది అని భావించండి
xmlDoc
లో - కొత్త అంశ నోడ్ <edition> సృష్టించండి
- టెక్స్ట్ ను కలిగిన కొత్త టెక్స్ట్ నోడ్ సృష్టించండి
"first"
- ఈ కొత్త టెక్స్ట్ నోడ్ ను కొత్త అంశ నోడ్ కు జోడించండి
- కొత్త అంశ నోడ్ ను మొదటి <book> మెటాకు జోడించండి
ఉదాహరణ 2
టెక్స్ట్ నోడ్స్ కలిగిన అంశ నోడ్స్ ను అన్ని <book> మెటాలకు జోడించండి:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("మొదటి ఎడిషన్"); newEle.appendChild(newText); x[i].appendChild(newEle); }
CDATA సెక్షన్ నోడ్ సృష్టించండి
createCDATASection()
కొత్త CDATA సెక్షన్ నోడ్స్ సృష్టించే మాదిరిగా ఉపయోగించండి.
ఉదాహరణ 1
newCDATA = xmlDoc.createCDATASection("నూతన సంవత్సర ప్రత్యేక పెట్టుబడులు & పరిమిత డిస్కౌంట్"); xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
ఉదాహరణ వివరణం:
- ఇక్కడ బుక్స్ ఎక్స్ఎల్ ప్రదర్శించబడింది అని భావించండి
xmlDoc
లో - కొత్త CDATA సెక్షన్ నోడ్ సృష్టించండి
- ఈ కొత్త CDATA నోడ్ ను మొదటి <book> మెటాకు జోడించండి
ఉదాహరణ 2
సమిక్షించండి మరియు అన్ని <book> మెటాలకు CDATA భాగాలను జోడించండి:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length; newtext = "నూతన సంవత్సర ప్రత్యేక పెట్టుబడులు & పరిమిత డిస్కౌంట్"; for (i = 0; i < xLen; i++) { newCDATA = xmlDoc.createCDATASection(newtext); x[i].appendChild(newCDATA); }
కామెంట్ నోడ్ సృష్టించండి
createComment()
కొత్త కామెంట్ నోడ్స్ సృష్టించే మాదిరిగా ఉపయోగించండి.
ఉదాహరణ 1
newComment = xmlDoc.createComment("2024 年 2 月修订"); xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
ఉదాహరణ వివరణం:
- ఇక్కడ బుక్స్ ఎక్స్ఎల్ ప్రదర్శించబడింది అని భావించండి
xmlDoc
లో - కొత్త కామెంట్ నోడ్ సృష్టించండి
- ఈ కొత్త కామెంట్ నోడ్ ను మొదటి <book> మెటాకు జోడించండి
ఉదాహరణ 2
సమిక్షించండి మరియు అన్ని <book> మెటాలకు కామెంట్ నోడ్స్ జోడించండి:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length for (i = 0; i < xLen; i++) { newComment = xmlDoc.createComment("2024 年 2 月修订"); x[i].appendChild(newComment); }