Attribute is considered to indicate "What is a thing", and the method is to explain "What is a thing to do ".
Some typical Dom attributes include: nodename node name
Nodevalue node Value
Parent node of the parentnode Node
Direct point set of the childnodes Node
Attribute Set of the attributes Node
Some typical Dom methods include getelementsbytagname (name)-Get all elements with the specified tag name
X. appendchild (node)-insert a subnode to X
X. removechild (node)-delete a subnode from X
For example, there is a web. xml file.
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description id="test">This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>testService</servlet-name> <servlet-class>com.treetest.testService</servlet-class> </servlet> <servlet-mapping> <servlet-name>testService</servlet-name> <url-pattern>/servlet/testService</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Now you need to load the XML file on the page and read the content.
<SCRIPT type = "text/JavaScript"> function loadxmldoc (xmlname) {try {xmldom = new activexobject ("Microsoft. xmldom "); // IE browser} catch (e) {try {xmldom = document. implementation. createdocument ("", "", null); // non-IE browser} catch (e) {alert (E. message) ;}} try {xmldom. async = false; // whether to asynchronously load xmldom. load (xmlname); // load the content of the XML file return (xmldom);} catch (e) {alert (E. message) ;}} function test () {var xmldoc = loadxmldoc ("Web. XML "); var tagname = xmldoc. getelementsbytagname ("servlet"); // obtain the label alert (tagname [0]. childnodes [0]. text); // obtain the text of the first subnode of the first Node object. The result is: this is the description of my J2EE component alert (tagname [0]. childnodes [0]. getattribute ("ID"); // obtain the ID attribute value of the first subnode of the first Node object. The result is: Test alert (tagname [0]. childnodes [0]. attributes [0]. value); // The result of obtaining the ID property value is: Test} </SCRIPT>
You can also obtain the root node of the XML file, obtain the child node based on the root node, and gradually go deep into the data you want. The method for obtaining the root node is documentelement.
For example
Function Test () {var xmldoc = loadxmldoc ("Web. XML "); // var tagname = xmldoc. getelementsbytagname ("servlet"); var tagname = xmldoc.doc umentelementalert (tagname. childnodes [0]. childnodes [0]. text); // The result is: this is the description of my J2EE componentalert (tagname. childnodes [0]. childnodes [0]. getattribute ("ID"); // The result is: Test}