The previous article introduced how javascript accesses nodes.
This article describes how JavaScript operates nodes (including adding, deleting, modifying, and querying nodes ).
Based on the previous introduction, we know that the XML document mainly contains element nodes, attribute nodes, and text nodes.
The following describes in detail how JavaScript operates on them.
Element Node:
Search: Introduced in the previous article. It mainly finds and locates through getelementsbytagname.
Example:
// Output all titles
VaR T = xmldoc. getelementsbytagname ("title ");
For (I = 0; I <t. length; I ++)
{
Document. Write (T [I]. childnodes [0]. nodevalue );
Document. Write ("<br/> ");
}
Add: Create an element using createelement and appendchild to add a subnode.
Example:
VaR newnode = xmldoc. createelement ("new element name"); // create an element node
VaR nodebook = xmldoc. getelementsbytagname ("book") [0]; // locate the node book
Nodebook. appendchild (newnode); // append newnode as a subnode to the subnode of the parent node book. That is to say, to append a node, you must first find the parent node.
You can also add nodes by cloning them.
The clonenode () method has a parameter (true or false ). This parameter indicates whether the copied node includes all attributes and subnodes of the original node.
Example:
Oldnode = xmldoc. getelementsbytagname ('book') [0];
Newnode = oldnode. clonenode (true); // clone and copy the original node and all attributes and subnodes
Xmldoc.doc umentelement. appendchild (newnode );
Delete: The parent node calls removechild.
For example:
VaR nodebook = xmldoc. getelementsbytagname ("book") [0]; // locate the node book
Xmldoc.doc umentelement. removechild (nodebook); // Delete the first book node under the root node
Note: When a node is deleted, its subnodes are also deleted.
Modify: Directly modifying elements is not allowed. You can use replaceChild to modify the modification.
Syntax: parentnode. replaceChild (newnode, oldnode)
In addition, the element does not have nodevalue. To modify the text in the element, such as modifying the hello in <title> Hello </title>, refer to the following operationsCompositionThis node.
Text node:
Search: You cannot use getelementsbytagname to search for a text node. Instead, you can use it to find the element node, then, use childnodes [0] Or firstchild to locate the text node (because it exists in the form of the first subnode of the element node), and then use nodevalue to obtain the text content.
Example:
VaR nodetitle = xmldoc. getelementsbytagname ("title") [0];
VaR titletextnode = nodetitle. childnodes [0]; // you can also use firstchild
VaR thetxt = titletextnode. nodevalue;
Add: It is similar to the method for adding elements, but the text is created through createtextnode (Note: You can also add the content in XHTML using innerhtml ).
Example:
VaR edition = xmldoc. createelement ("edition ");
VaR newtext = xmldoc. createtextnode ("this is first ");
Edition. appendchild (newtext); // when operating XHTML, the above two rows can be directly replaced by edition. innerhtml = 'this is first;
VaR nodebook = xmldoc. getelementsbytagname ("book") [0];
Nodebook. appendchild (Edition );
Delete: The parent node calls removechild. Of course, nodevalue can also be cleared. For example, textnode. nodevalue = ''.
Modify: Textnode. nodevalue = 'modify to the text content you want '.
In addition, you can use replacedata () to replace data in a text node.
The replacedata () method has three parameters:
Offset-where to start replacement characters. The offset value starts with 0.
Length-the number of characters to replace .,
String-the string to be inserted.
Example:
Xmldoc. getelementsbytagname ("title") [0]. childnodes [0]. replacedata (0, 8, "hello ");
// Note: in fact, you can use substr or substring to process the string in advance and assign it to nodevalue.
Attribute node:
Search: Unlike element nodes, attribute nodes have text values. The method to get the attribute value is to get its text value. You can use the getattribute () method or the nodevalue attribute of the attribute node to complete this task.
Example:
Xmldoc. getelementsbytagname ("title") [0]. getattribute ("Lang"); // return "en"
Or
Xmldoc. getelementsbytagname ("title") [0]. getattributenode ("Lang"). nodevalue; // return "en"
Add: Use setattribute or setattributenode.
Example:
Xmldoc. getelementsbytagname ('book') [0]. setattribute ("edition", "this is first ");
Or
VaR newatt = xmldoc. createattribute ("edition ");
Newatt. nodevalue = "this is first ";
Xmldoc. getelementsbytagname ("title") [0]. setattributenode (newatt );
Delete: Removeattribute (name) or removeattributenode (node) are available)
Example:
// Method 1: removeattribute
// Delete the "category" attribute in the first <book> element:
Xmldoc. getelementsbytagname ("book") [0]. removeattribute ("category ");
// Method 2: removeattributenode
// Delete all attributes of all <book> Elements
X = xmldoc. getelementsbytagname ("book ");
For (I = 0; I <X. length; I ++)
{
While (X [I]. Attributes. length> 0)
{
Attnode = x [I]. attributes [0];
Old_att = x [I]. removeattributenode (attnode );
}
}
Modify: Use the setattribute () method or the nodevalue attribute of the attribute node.
Example:
// Setattribute
Xmldoc. getelementsbytagname ('book') [0]. setattribute ("category", "child ");
// Set nodevalue
VaR x = xmldoc. getelementsbytagname ("book") [0]
Var y = x. getattributenode ("category ");
Y. nodevalue = "child ";
conclusion: (To be continued)