XML DOM ਨੋਟਸ ਬਣਾਉਣਾ
ਨਵਾਂ ਨੋਡ ਬਣਾਉਣਾ
createElement()
ਮੇਥੋਡ ਨਵਾਂ ਨੋਡ ਬਣਾਉਂਦਾ ਹੈ:
ਉਦਾਹਰਨ 1
newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
ਉਦਾਹਰਨ ਵਿਸਥਾਰ:
- ਸਮਝਦੇ ਹੋਏ books.xml ਨੂੰ ਲੋਡ ਕੀਤਾ ਗਿਆ ਹੈ
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 = "first edition"; 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 = "first edition"; x[i].setAttributeNode(newAtt); }
ਜੇਕਰ ਅਟਰੀਬਿਊਟ ਪਹਿਲਾਂ ਹੈ ਤਾਂ ਉਸ ਨੂੰ ਨਵੇਂ ਅਟਰੀਬਿਊਟ ਨਾਲ ਬਦਲ ਦਿੰਦਾ ਹੈ。
setAttribute() ਨਾਲ ਅਟਰੀਬਿਊਟ ਬਣਾਉਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ
ਕਿਉਂਕਿ setAttribute()
ਮੇਥੋਡ ਅਟਰੀਬਿਊਟ ਨਹੀਂ ਮਿਲਣ ਤੱਕ ਨਵਾਂ ਅਟਰੀਬਿਊਟ ਬਣਾਉਂਦਾ ਹੈ ਇਸ ਲਈ ਇਹ ਨਵੇਂ ਅਟਰੀਬਿਊਟ ਬਣਾਉਣ ਲਈ ਵੀ ਵਰਤਿਆ ਜਾ ਸਕਦਾ ਹੈ。
ਉਦਾਹਰਨ 1
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
ਉਦਾਹਰਨ ਵਿਸਥਾਰ:
- ਸਮਝਦੇ ਹੋਏ books.xml ਨੂੰ ਲੋਡ ਕੀਤਾ ਗਿਆ ਹੈ
xmlDoc
ਵਿੱਚ - ਪਹਿਲੇ <book> ਨੋਡ ਦਾ
"edition"
ਅਟਰੀਬਿਊਟ ਦਾ ਮੁੱਲ ਸੈਟ ਕਰੋ:"first"
ਉਦਾਹਰਨ 2
ਸਾਰੇ <title> ਨੋਡਾਂ ਨੂੰ ਪਰਖ ਕਰਨ ਅਤੇ ਨਵਾਂ ਅਟਰੀਬਿਊਟ ਜੋੜਨ:
for(i = 0; i < x.length; i++) { x[i].setAttribute("edition", "first edition"); }
ਟੈਕਸਟ ਨੋਡ ਬਣਾਉਣਾ
createTextNode()
ਮੇਥੋਡ ਨਵਾਂ ਟੈਕਸਟ ਨੋਡ ਬਣਾਉਂਦਾ ਹੈ:
ਉਦਾਹਰਨ 1
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
ਉਦਾਹਰਨ ਵਿਸਥਾਰ:
- ਸਮਝਦੇ ਹੋਏ books.xml ਨੂੰ ਲੋਡ ਕੀਤਾ ਗਿਆ ਹੈ
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 Section ਨੂੰ ਬਣਾਓ
createCDATASection()
ਨਵੀਂ CDATA section ਨੂੰ ਬਣਾਉਣ ਵਾਲੀ ਮਹੱਤਵਪੂਰਨ ਮੰਦਰਘਟੜੀ ਹੈ
ਉਦਾਹਰਨ 1
newCDATA = xmlDoc.createCDATASection("新年特惠 & 限时折扣"); xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
ਉਦਾਹਰਨ ਵਿਸਥਾਰ:
- ਸਮਝਦੇ ਹੋਏ books.xml ਨੂੰ ਲੋਡ ਕੀਤਾ ਗਿਆ ਹੈ
xmlDoc
ਵਿੱਚ - ਨਵੀਂ CDATA section ਨੂੰ ਬਣਾਓ
- ਨਵੀਂ 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);
ਉਦਾਹਰਨ ਵਿਸਥਾਰ:
- ਸਮਝਦੇ ਹੋਏ books.xml ਨੂੰ ਲੋਡ ਕੀਤਾ ਗਿਆ ਹੈ
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); }