HTML file:
CopyCode The Code is as follows: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> add, delete, modify, and query JS operations in XML (under IE) </title>
<SCRIPT type = "text/JavaScript"> <! --
/* And other issues solved:
1. What layer does XPath locate? For example, root level or person or name level.
*/
VaR xmldoc;
VaR rootnode; // Root Node
// Load the XML document
Function loadxml (){
Try {
Xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = false; // disable asynchronous loading
Xmldoc. Load ("xmlfile. xml"); // load is from file, loadxml is from string.
Rootnode = xmldoc.doc umentelement;
} Catch (e) {alert (E. Message )}
}
// Display XML documents in memory
Function outxml (){
VaR divxml = Document. getelementbyid ("divxml ");
Divxml. innerhtml = xmldoc. xml; // display XML content. The trick is to add an XML suffix .?
Alert (xmldoc. XML );
}
// Add
Function addxml (){
// Set the text value for the leaf node
VaR newname = xmldoc. createelement ("name ");
Newname. Text = "Crane ";
VaR newgender = xmldoc. createelement ("gender ");
Newgender. Text = "female ";
// Parent node, with appendchild (childnode );
VaR newperson = xmldoc. createelement ("person ");
// Set the property ID
Newperson. setattribute ("ID", "2 ");
Newperson. appendchild (newname );
Newperson. appendchild (newgender );
// Add to the root node
Rootnode. appendchild (newperson );
Alert (xmldoc. XML );
}
// Delete
Function deletexml (){
// Locate the node first
VaR singlenode = xmldoc. selectsinglenode ("/root/person [name = 'tree']");
// Find the parent and delete it.
Singlenode. parentnode. removechild (singlenode );
Alert (xmldoc. XML );
}
// Modify
Function updatexml (){
VaR singlenode = xmldoc. selectsinglenode ("/root/person [name = 'crane ']");
Singlenode. childnodes [0]. Text = "updated ";
Alert (xmldoc. XML );
}
// Query
Function queryxml (){
// Alert (rootnode. nodename); // node name
// Alert (rootnode. Text); // all content in the node
// Select the node Array Using xpath
// Var nodes = xmldoc. selectnodes ("/root/person ");
// Alert (nodes [0]. Text );
// Select a single node
/* Summary
1. "/root/person [name = 'tree ']" is equivalent to "/root [person/name = 'tree']", that is, the person node is found.
*/
VaR singlenode = xmldoc. selectsinglenode ("/root/person [Gender = 'female']"); // The value must be enclosed in quotation marks.
Alert (singlenode. Text );
Alert (singlenode. getattribute ("ID "));
// Test XPath Positioning
VaR sglnode = xmldoc. selectsinglenode ("/root [person/gender = 'male']"); // The location is not clear here. study again.
Alert (sglnode. Text );
// Display all XML documents
// Alert (xmldoc. XML );
}
// --> </SCRIPT>
</Head>
<Body>
<Div id = "divxml"> </div>
<Input type = "button" value = "LOAD" onclick = "loadxml ();"/>
<Input type = "button" value = "show" onclick = "outxml ();"/>
<Input type = "button" value = "add" onclick = "addxml ();"/>
<Input type = "button" value = "delete" onclick = "deletexml ();"/>
<Input type = "button" value = "Update" onclick = "updatexml ();"/>
<Input type = "button" value = "query" onclick = "queryxml ();"/>
</Body>
</Html>
XML file: copy Code the code is as follows:
tree
male