JavaScript XML additions and deletions (IE) operations to achieve code _JAVASCRIPT skills

Source: Internet
Author: User
Tags xpath
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 ">
&LT;TITLE&GT;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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.