XML DOM Add Node


实例

Try it Yourself - Example

The example below uses an XML filebooks.xml。
FunctionloadXMLDoc(), Located atExternal JavaScript Among them,used foraddload XML File. → , located in external JavaScript, used to load the XML file.

Add a node after the last child node
bookExampleUsage appendChild() Methodstowarda已Yesofnodeaddachild node。 → This example uses the appendChild() method to add a child node to an existing node.

Add a node before the specified child node
bookExampleUsage insertBefore() MethodsInaspecifiedchild nodebeforeInsertanode。 → This example uses the insertBefore() method to insert a node before a specified child node.

Add a new attribute
bookExampleUsage setAttribute() MethodsAdd a new attribute。 → This example uses the setAttribute() method to add a new attribute.

Add data to a text node
bookExampleUsage insertData() holdDataInserta已YesofText NodeIn. → This example uses insertData() to insert data into an existing text node.


Add Node - appendChild()

appendChild() Methodstowarda已Yesofnodeaddachild node。 → The appendChild() method adds a child node to an existing node.

The new node is added (appended) after any existing child nodes.

Note:ifnode'sPosition很Important,pleaseUsage insertBefore() method. → If the position of the node is important, use the insertBefore() method.

belowofCode snippetcreate aelement(<edition>), anditadd tofirst <book> element's最afterachild nodebehind: → The following code snippet creates an element (<edition>) and adds it after the last child node of the first <book> element:

Example

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Create a new node <edition> → Create a new node <edition>
  3. holdThisnodeAppendtofirst <book> element → Append this node to the first <book> element

traversalandtowardAll <book> elementAppendaelement: → Loop through and append an element to all <book> elements:Try it Yourself


Inserting Nodes - insertBefore()

insertBefore()Methodsused forInspecifiedchild nodebeforeInsertnode。 → The insertBefore() method is used to insert a node before a specified child node.

InByaddofnode'sPosition很Importantwhen,thisMethods很Yesuse: → This method is useful when the position of the node being added is important:

Example

xmlDoc=loadXMLDoc("books.xml");

newNode=xmlDoc.createElement("book");

x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];

x.insertBefore(newNode,y);

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. create aNewelementnode <book> → Create a new element node <book>
  3. Insert this new node before the last <book> element node.

if insertBefore() of the二itemsParameterYes null,NewnodewillByadd to最aftera已YesOf subnodeafter。 → If the second parameter of insertBefore() is null, the new node will be added after the last existing child node.

x.insertBefore(newNode,null)andx.appendChild(newNode)Both can append a new child node to x.


Add new attribute

addAtribute() ThisMethodsYesDoes not exist. → The method addAtribute() does not exist.

ifPropertyDoes not exist, then setAttribute() cancreate aNewAttributes: → If the attribute does not exist, setAttribute() can create a new attribute:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. holdfirst <book> element's "edition" Propertyvalue ofSettings(Create)is "first" → Set (create) the value of the "edition" attribute of the first <book> element to "first"

Note:ifProperty已Exists,setAttribute() Methodswill覆盖已Yesthe value of. → If the attribute already exists, the setAttribute() method will overwrite the existing value.


towardText NodeaddText - insertData() → Add text to a text node - insertData()

insertData() MethodswillDataInsert已YesofText NodeIn. → The insertData() method inserts data into an existing text node.

The insertData() method has two parameters:

  • offset - Inwhat处startInsertCharacter(with 0 start) → offset - where to start inserting characters (starting from 0)
  • string - wantInsertthe string → string - the string to insert

belowofCode snippetwillput "Easy" add to已addloadof XML of thea <title> element'sText Node: → The following code snippet will add "Easy" to the text node of the first <title> element of the loaded XML:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

x.insertData(0,"Easy ");

Try it Yourself »

other extensions