إضافة عنصر XML DOM
- الصفحة السابقة إنشاء عقد DOM
- الصفحة التالية كرر العقد DOM
مثال
مثال أدناه يستخدم ملف XML books.xml.
دالة loadXMLDoc()،موجود خارج JavaScript،لتحميل ملف XML.
- Add a node after the last child node
- This example uses the appendChild() method to add a child node to an existing node.
- Add a node before a specified child node
- This example uses the insertBefore() method to insert a node before a specified child node.
- Add a new attribute
- This example uses the setAttribute() method to add a new attribute.
- Add data to a text node
- This example uses insertData() to insert data into an existing text node.
Add node - appendChild()
The appendChild() method adds a child node to an existing node.
The new node will be added (appended) after any existing child node.
ملاحظة:If the position of the node is very important, please use the insertBefore() method.
The following code snippet creates an element (<edition>) and adds it to the last child node of the first <book> element:
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
مثال توضيحي:
- من خلال استخدام loadXMLDoc() أضف "books.xml"تحميل xmlDoc"
- Create a new node <edition>
- Append this node to the first <book> element
Loop and append an element to all <book> elements:TIY
Insert node - insertBefore()
The insertBefore() method is used to insert a node before a specified child node.
This method is very useful when the position of the node to be added is very important.
xmlDoc=loadXMLDoc("books.xml"); newNode=xmlDoc.createElement("book"); x=xmlDoc.documentElement; y=xmlDoc.getElementsByTagName("book")[3]; x.insertBefore(newNode,y);
مثال توضيحي:
- من خلال استخدام loadXMLDoc() أضف "books.xml"تحميل xmlDoc"
- Create a new element node <book>
- Insert this node before the last <book> element node
If the second parameter of insertBefore() is null, the new node will be added after the last existing child node.
x.insertBefore(newNode,null) and x.appendChild(newNode) All can append a new child node to x.
Add a new attribute
method addAtribute() does not exist.
إذا كانت الخاصية غير موجودة، يمكن لـ setAttribute() إنشاء خاصية جديدة:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("edition","first");
مثال توضيحي:
- من خلال استخدام loadXMLDoc() أضف "books.xml"تحميل xmlDoc"
- أضف قيمة الخاصية "edition" للعقد الأول من <book> إلى "first"
ملاحظة:إذا كانت الخاصية موجودة، فإن طريقة setAttribute() ستغطي القيمة الموجودة بالفعل.
إضافة نص إلى نود النص - insertData()
تضيف طريقة insertData() بيانات إلى النود النصي الموجود.
يملك طريقة insertData() إثنين من المعلمات:
- offset - حيث يبدأ الإدراج النصي (يبدأ من 0)
- string - النص الذي سيتم إدراجه
النص التالي سيضيف "Easy" إلى نود النص الخاص بالعنوان الأول من XML المحمول:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.insertData(0,"Hello ");
- الصفحة السابقة إنشاء عقد DOM
- الصفحة التالية كرر العقد DOM