Home  >  Article  >  Backend Development  >  XML Learning (2) Detailed explanation of DOM operations on XML documents

XML Learning (2) Detailed explanation of DOM operations on XML documents

黄舟
黄舟Original
2017-03-20 16:56:091557browse

In the eyes of DOM, HTML is a tree-structured document like XML. Each node is treated as a Node object of various types. Each Node object has its own properties and methods, which can be used to traverse the entire document tree. DOM defines nodeType to represent the type of node

##Text Node.TEXT_NODE##Node.COMMENT_NODE8Commented text##DocumentFragmentAttr

The root node of the DOM tree is a Document object, and sometimes the document points to the entire document.

Most of the methods defined by Document are production methods, mainly used to create various types of nodes that can be inserted into the document. Commonly used Document methods are:

Interface

##nodeType Constant

nodeType value

Remarks

Element

Node.ELEMENT_NODE

1

##Element Node

##3

Text Node

Document

##Node.DOCUMENT_NODE

9

##document

Comment

Node.DOCUMENT_FRAGMENT_NODE

##11

document fragment

Node.ATTRIBUTE_NODE

##2

Node attributes

Commonly used methods of Element:

Method

Description

createAttribute()

Create with the specified name New Attr node.

createComment()

Use the specified string Create a new Comment node.

createElement()

Use the specified tag name Create a new Element node.

createTextNode()

Create with the specified text New TextNode node.

##getElementById()

Returns the Element node with the specified id attribute in the document.

##getElementsByTagName()

Returns the document with the specified All Element nodes of the tagname.

## Returns a Node##hasAttribute()removeAttribute()##setAttribute()Set the specified attribute For the specified string value, adds a new attribute if it does not exist. ##setAttributeNode()

##Method

Description

getAttribute()

Returns the value of the specified property as a string.

getAttributeNode()

In the form of Attr node Returns the value of the specified property.

##getElementsByTabName()

Array

Contains the descendant nodes of all Element nodes with the specified tag name, in the order they appear in the document.

If the element has the specified Name attribute, returns true.

Remove the specified attribute from the element.

##removeAttributeNode()

From the attribute list of the element Delete the specified Attr node.

Set the specified Attr node Added to the element's attribute list.

Commonly used attributes of Node objects:

##Return the next node in the form of Node The current node and its previous sibling nodes. If there is no such node, null is returned.

Common methods of Node objects:

Attributes

Description

##attributes

If the node is an Element, the attributes of the element are returned in the form of NamedNodeMap.

##childNodes

is stored in the form of Node[] The child nodes of the current node. If there are no child nodes, an empty array is returned.

firstChild

Returns the current node in the form of Node the first child node.

null if there are no child nodes.

lastChild

Returns the current node in the form of Node the last child node of . This is null if there are no child nodes.

nextSibling

Returns the next node that is the sibling of the current node in the form of Node. If there is no such node, null is returned.

nodeName

The name of the node, the Element node is Represents the tag name of the Element.

nodeType

represents the type of node.

##parentNode

Returns the current node in the form of Node 's parent node. If there is no parent node, this is null.

##previousSibling

Method

Description

##appendChild()

By adding a node to The childNodes[] group of the current node adds nodes to the document tree.

##cloneNode()

Copy the current node, or Copies the current node and all its descendant nodes.

hasChildNodes()

If the current node has children node, it will return true.

insertBefore()

Insert a document tree Node, positioned before the specified child node of the current node. If the node already exists, delete it and insert it in its place.

removeChild()

Remove from the document tree and returns the specified child node.

replaceChild()

Removes and returns the specified child node from the document tree , replace it with another node.


Getting the value of an element

The following code retrieves the text value of the first b2386ffb911b14667cb8f0f91ea547a7 element:

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];txt=x.nodeValue; //其实应该先判断节点是否存在

Result: txt = "Harry Potter"

Get the value of the attribute

The following code retrieves the "lang# of the first b2386ffb911b14667cb8f0f91ea547a7 element ##" Text value of the attribute:

txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Result: txt = "en"

Change the value of the element

The following code changes the first b2386ffb911b14667cb8f0f91ea547a7 element Text value:

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];x.nodeValue="Easy Cooking";

Change the value of the attribute

The setAttribute() method can be used to change the value of an existing attribute, or create a new attribute.

The following code adds a new attribute named "edition" (with a value of "first") to each 463aef0d2da08708f472268a99530dbe element:

x=xmlDoc.getElementsByTagName("book");for(i=0;i<x.length;i++)  {  x[i].setAttribute("edition","first");  }

Creating elements

The createElement() method creates a new element node.

createTextNode() method creates a new text node.

appendChild() method adds a child node to a node (after the last child node).

If you need to create a new element with text content, you need to create an element node and a text node at the same time.

The following code creates an element (a866a342e5caa631fbe389b07113afd3) and then adds it to the first 463aef0d2da08708f472268a99530dbe element:

newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");x[0].appendChild(newel);

Example explanation:

  1. Create a866a342e5caa631fbe389b07113afd3 element

  2. Create a text node with a value of "First"

  3. Place this text node Append to a866a342e5caa631fbe389b07113afd3 element

  4. Append a866a342e5caa631fbe389b07113afd3 element to first 463aef0d2da08708f472268a99530dbe element

Delete element

removeChild() method deletes the specified node (or element).

The following code snippet will delete the first node in the first 463aef0d2da08708f472268a99530dbe element:

x=xmlDoc.getElementsByTagName("book")[0];x.removeChild(x.childNodes[0]);

Example code

<p><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff;"><</span><span style="color: #800000;">html </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.w3.org/1999/xhtml"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">head </span><span style="color: #ff0000;">runat</span><span style="color: #0000ff;">="server"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">title</span><span style="color: #0000ff;">></</span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/<a href="http://www.php.cn/wiki/48.html" target="_blank">javascript</a>"</span><span style="color: #0000ff;">></span><span style="background-color: #f5f5f5; color: #000000;"><br/></span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> parseXML() {<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">Internet Explorer</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            {<br/>               xmlDoc </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">new</span><span style="background-color: #f5f5f5; color: #000000;"> ActiveX<a href="http://www.php.cn/wiki/60.html" target="_blank">Object</a>(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">Microsoft.XMLDOM</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>            }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;"> (e) {              <br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">Firefox, Mozilla, Opera, etc.</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                {<br/>                    xmlDoc </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> document.implementation.createDocument(</span><span style="background-color: #f5f5f5; color: #000000;">""</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">""</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #0000ff;">null</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;"> (e) {<br/>                    alert(e.message);<br/></span><span style="background-color: #f5f5f5; color: #0000ff;"><a href="http://www.php.cn/wiki/135.html" target="_blank">return</a></span><span style="background-color: #f5f5f5; color: #000000;">;<br/>                }<br/>            }<br/>            xmlDoc.async </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">; </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">假如xml载入完毕执行以下</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            xmlDoc.load(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">note.xml</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> x </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> xmlDoc.getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">to</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">            xmlDoc.getElementsByTagName("to")[0].setAttribute("<a href="http://www.php.cn/wiki/1255.html" target="_blank">date</a>") = "1515";  //设置属性值</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">            xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue = "tiewang"; //设置文本节点值</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            <br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">            //<a href="http://www.php.cn/code/6276.html" target="_blank">循环</a>修改</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">            for (i = 0; i < x.length; i++) {</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                if (x[i].childNodes[0] == null) { //如果不存在问题,则添加</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                    newText1 = xmlDoc.createTextNode("af");</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                    x[i].appendChild(newText1);</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                }</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                x[i].childNodes[0].nodeValue = "af";</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">            }</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">            //循环添加 //注意,如果把create放在<a href="http://www.php.cn/code/5913.html" target="_blank">for循环</a>外面,则只能添加到最后一个,需要放在for里面</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            </span><span style="background-color: #f5f5f5; color: #0000ff;">for</span><span style="background-color: #f5f5f5; color: #000000;"> (i </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">; i </span><span style="background-color: #f5f5f5; color: #000000;"><</span><span style="background-color: #f5f5f5; color: #000000;"> x.length; i</span><span style="background-color: #f5f5f5; color: #000000;">++</span><span style="background-color: #f5f5f5; color: #000000;">) {<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;"> (x[i].getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">too</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">)[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">] </span><span style="background-color: #f5f5f5; color: #000000;">==</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">null</span><span style="background-color: #f5f5f5; color: #000000;">) {<br/>                    newElemenet </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> xmlDoc.createElement(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">too</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                    newText </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> xmlDoc.createTextNode(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">logo</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                    newElemenet.setAttribute(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">id</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">1</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                    newElemenet.appendChild(newText);       <br/>                    x[i].appendChild(newElemenet);<br/>                }<br/>            }<br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">            //循环删除</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">            for (i = 0; i < x.length; i++) {</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                if (x[i].getElementsByTagName("too")[0] != null) {</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                    x[i].removeChild(x[i].getElementsByTagName("too")[0]);</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">                }</span><span style="background-color: #f5f5f5; color: #008000;"><br/>//</span><span style="background-color: #f5f5f5; color: #008000;">            }</span></p><p><span style="background-color: #f5f5f5; color: #008000;"> </span><span style="background-color: #f5f5f5; color: #008000;">    <br/></span><span style="background-color: #f5f5f5; color: #000000;">           <span style="font-family: Courier New; background-color: #f5f5f5;">      </span></span></p><p><span style="background-color: #f5f5f5; color: #000000;"><span style="font-family: Courier New; background-color: #f5f5f5;">      //删除to -> too 节点中的ID属性<br/>            for (i = 0; i < x.length; i++) {<br/>                if (x[i].getElementsByTagName("too")[0] != null) {<br/>                    if (x[i].getElementsByTagName("too")[0].getAttribute("id")!=null) {<br/>                        x[i].getElementsByTagName("too")[0].removeAttribute("id");<br/>                    }<br/>                }<br/>            }</span><br/></span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> xmlHttp;<br/><br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> {<br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> Firefox, Opera 8.0+, Safari</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                xmlHttp </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">new</span><span style="background-color: #f5f5f5; color: #000000;"> XMLHttpRequest();<br/>            }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;"> (e) {<br/><br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> Internet Explorer</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                </span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> {<br/>                    xmlHttp </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">new</span><span style="background-color: #f5f5f5; color: #000000;"> ActiveXObject(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">Msxml2.XMLHTTP</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;"> (e) {<br/><br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> {<br/>                        xmlHttp </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">new</span><span style="background-color: #f5f5f5; color: #000000;"> ActiveXObject(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">Microsoft.XMLHTTP</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/>                    }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;"> (e) {<br/>                        alert(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">您的浏览器不支持AJAX!</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">return</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">;<br/>                    }<br/>                }<br/>            }<br/>            xmlHttp.onreadystatechange </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;">() {    </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">onreadystatechange 属性存有处理服务器响应的<a href="http://www.php.cn/wiki/145.html" target="_blank">函数</a></span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                </span><span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;"> (xmlHttp.readyState </span><span style="background-color: #f5f5f5; color: #000000;">==</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #000000;">4</span><span style="background-color: #f5f5f5; color: #000000;">) {       </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">readyState 属性存有服务器响应的<a href="http://www.php.cn/code/8243.html" target="_blank">状态</a>信息</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                    document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">to</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).innerHTML </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> xmlHttp.responseText; </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">通过 responseText 属性来取回由服务器返回的数据</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">                }<br/>            }<br/>            xmlHttp.open(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">POST</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">receive.<a href="http://www.php.cn/wiki/1519.html" target="_blank">asp</a>x?type=xmlsave</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">, </span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">); <br/><br/></span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> open() 方法需要三个参数。第一个参数定义发送请求所使用的方法(GET 还是 POST)。第二个参数规定服务器端脚本的 URL。第三个参数规定应当对请求进行异步地处理。</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">            xmlHttp.send(xmlDoc);      </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">send() 方法可将请求送往服务器     </span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;">    }<br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">body </span><span style="color: #ff0000;"><a href="http://www.php.cn/wiki/1467.html" target="_blank">onload</a></span><span style="color: #0000ff;">="parseXML()"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">form </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="form1"</span><span style="color: #ff0000;"> runat</span><span style="color: #0000ff;">="server"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">h1 </span><span style="color: #ff0000;">id </span><span style="color: #0000ff;">= "to"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">h1 </span><span style="color: #ff0000;">id </span><span style="color: #0000ff;">="value"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/><br/><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></p>

The above is the detailed content of XML Learning (2) Detailed explanation of DOM operations on XML documents. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn