Code for adding, deleting, modifying, and querying javascript Xml (under IE) Operations

Source: Internet
Author: User

Html file:
Copy codeThe 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 codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Root>
<Person 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.