HTML file:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<TITLE>JS operation XML Additions and deletions (ie) </title>
<script type= "Text/javascript" ><!--
/* and other issues to solve:
1.xpath exactly which layer to locate, and how to navigate to the level of such as root or person or name.
*/
var xmldoc;
var RootNode; Root node
Loading XML documents
function Loadxml () {
try{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
xmldoc.async=false;//Turn off asynchronous loading
Xmldoc.load ("XMLFile.xml");//load is from file, Loadxml is from string.
RootNode = xmldoc.documentelement;
}catch (e) {alert (e.message)}
}
Displaying an in-memory XML document
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);
}
Increase
function Addxml () {
Leaf node, set text value
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 Property ID
Newperson.setattribute ("id", "2");
Newperson.appendchild (NewName);
Newperson.appendchild (Newgender);
Add to root node
Rootnode.appendchild (Newperson);
alert (xmldoc.xml);
}
By deleting
function Deletexml () {
Find the knot first.
var singlenode = Xmldoc.selectsinglenode ("/root/person[name= ' tree ')");
Find Parent and delete
SingleNode.parentNode.removeChild (Singlenode);
alert (xmldoc.xml);
}
Change
function Updatexml () {
var singlenode = Xmldoc.selectsinglenode ("/root/person[name= ' Crane ')");
Singlenode.childnodes[0].text = "Updated";
alert (xmldoc.xml);
}
Check
function Queryxml () {
alert (rootnode.nodename);//Section Roll Call
alert (rootnode.text);//Everything in the node
XPath selection node array
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 '" which is found to be the person node.
*/
var singlenode = Xmldoc.selectsinglenode ("/root/person[gender= ' female ')")//The value here needs to be quoted
alert (Singlenode.text);
Alert (Singlenode.getattribute ("id"));
Testing XPath positioning
var sglnode = Xmldoc.selectsinglenode ("/root[person/gender= ' Male ')");//This position is unknown. Re-study.
alert (Sglnode.text);
Show all XML documents
alert (xmldoc.xml);
}
--></script>
<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>
XML file:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<per Son id= "1" >
<name>tree</name>
<gender>male</gender>
</person>
;/root>