一般從服務(wù)端的返回可以得到一個XML對象。
例如服務(wù)器返回的:XMLHttpRequest.ResponseXML
這里的XMLHttpRequest就是ajax的核心對象。
在IE下可以這樣創(chuàng)建:xmlHttp = new ActiveXObject("Microsoft.XMLHTTP").
javascript操作XML先創(chuàng)建一個XML DOM對象:var dom = new ActiveXObject("Microsoft.XMLDOM");
然后dom.loadXML(ResponseXML)就ok了。
接下來就可以操作xml,獲取內(nèi)容了。
一些常用的函數(shù)如下(一些在網(wǎng)上收集的,一些時平時老大教的):
Microsoft.XMLDOM 對象常用的屬性:
1、attributes 屬性,返回當前節(jié)點的屬性列表
2、childNodes 屬性,返回當前節(jié)點的所有子節(jié)點列表
3、documentElement 屬性,返回xml文件的根節(jié)點,通過Microsoft.XMLDOM對象名來調(diào)用
4、firstChild 屬性、lastChild 屬性,返回當前節(jié)點的第一個子(最后一個)元素(如果沒有子節(jié)點是不是返回
第一個屬性?)
5、nextSibling (previousSibling )屬性,下一個兄弟節(jié)點。
6、nodeName 屬性,返回節(jié)點的標簽名字
7、nodeValue 屬性,傳回指定節(jié)點相關(guān)的文字(不是屬性,就是*號的這個內(nèi)容 **)
8、ownerDocument 屬性,根節(jié)點
9、parentNode 屬性,傳回目前節(jié)點的父節(jié)點。只能應(yīng)用在有父節(jié)點的節(jié)點中。
搞一個例子:
function Add()
{
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.loadXML(ret);
if (dom.documentElement != null)
{
var nodes = dom.documentElement.selectNodes("http://SelectItem"); //得到根節(jié)點下所有SelectItem節(jié)點
if (nodes != null)
{
for(var i=0;i
一些常用的函數(shù):
1、AppendChild 方法,加上一個節(jié)點當作指定節(jié)點最后的子節(jié)點。
2、cloneNode(deep)方法,deep 是一個布爾值。如果為true,此節(jié)點會復制以指定節(jié)點發(fā)展出去的所有節(jié)
點。如果是false,只有指定的節(jié)點和它的屬性被復制。
3、createAttribute(name)方法,建立一個指定名稱的屬性。
4、createElement 方法,建立一個指定名稱的元素。
5、xmlDocument.createNode(type, name, nameSpaceURI);type 用來確認要被建立的節(jié)點型態(tài),name 是一個字符
串來確認新節(jié)點的名稱,命名空間的前綴則是選擇性的。nameSpaceURI 是一個定義命名空間URI 的字
符串。如果前綴被包含在名稱參數(shù)中,此節(jié)點會在nameSpaceURI 的內(nèi)文中以指定的前綴建立。如果不
包含前綴,指定的命名空間會被視為預設(shè)的命名空間。
6、getElementsByTagName 方法,傳回指定名稱的元素集合。
7、haschildnodes 方法,要解釋嗎?
8、insertBefore 方法,在指定的節(jié)點前插入一個子節(jié)點。xmlDocumentNode.insertBefore
(newChild,refChild);refChild 是參照節(jié)點的地址。新子節(jié)點被插到參照節(jié)點之前。如果refChild 參數(shù)沒有包含
在內(nèi),新的子節(jié)點會被插到子節(jié)點列表的末端。
9、load 方法和loadXML 方法,前這從url,后者從字符串片斷。
10、nodeFromID 方法,傳回節(jié)點ID 符合指定值的節(jié)點。
11、removeChild 方法和replaceChild(newChild,oldChild),顧名思義
12、selectNodes和selectSingleNode 方法,傳回所有符合提供樣式的節(jié)點。參數(shù)為一包含XSL 樣式的字符串。
以下收集了一些MSDN的例子
(1)
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo");
xmlDoc.appendChild(rootElement);(2) var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo");
rootElement.setAttribute("author", "Pat Coleman"); //屬性author的值為Pat Coleman
xmlDoc.appendChild(rootElement);
(3) var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo");
var memoAttribute=xmlDoc.createAttribute("author");
var memoAttributeText=xmlDoc.createTextNode("Pat Coleman");
memoAttribute.appendChild(memoAttributeText);
rootElement.setAttributeNode(memoAttribute);
xmlDoc.appendChild(rootElement);
//這個例子和(2)同樣效果,但是用不同的方法,這里把attribute也當做一個節(jié)點,attribute node的
子節(jié)點只可以是textnode,所以這里要先創(chuàng)建一個textnode在賦給他。
(4)
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var rootElement=xmlDoc.createElement("memo"); //創(chuàng)建一個元素
var memoAttribute=xmlDoc.createAttribute("author"); //創(chuàng)建一個屬性
var memoAttributeText=xmlDoc.createTextNode("Pat Coleman"); //創(chuàng)建一個文本節(jié)點
var toElement=xmlDoc.createElement("to"); //再創(chuàng)建一個元素
var toElementText=xmlDoc.createTextNode("Carole Poland"); //再創(chuàng)建一個文本節(jié)點
memoAttribute.appendChild(memoAttributeText);
xmlDoc.appendChild(rootElement);
rootElement.setAttributeNode(memoAttribute);
rootElement.appendChild(toElement);
toElement.appendChild(toElementText);
屬性:
attributes
Contains the list of attributes for this node. Read-only.
baseName
*Returns the base name for the name qualified with the namespace. Read-only.
childNodes
Contains a node list containing the children nodes. Read-only.
dataType
*Specifies the data type for this node. Read/write.
definition
*Returns the definition of the node in the document type definition (DTD) or schema. Read-only.
firstChild
Contains the first child of the node. Read-only.
lastChild
Returns the last child node. Read-only.
name
Contains the attribute name. Read-only.
namespaceURI
*Returns the Uniform Resource Identifier (URI) for the namespace. Read-only.
nextSibling
Contains the next sibling of this node in the parent‘s child list. Read-only.
nodeName
Contains the qualified name of the element, attribute, or entity reference, or a fixed string for other node types. Read-only.
nodeType
Specifies the XML Document Object Model (DOM) node type, which determines valid values and whether the node can have child nodes. Read-only.
nodeTypedValue
*Contains the node value expressed in its defined data type. Read/write.
nodeTypeString
*Returns the node type in string form. Read-only.
nodeValue
Contains the text associated with the node. Read/write.
ownerDocument
Returns the root of the document that contains the node. Read-only.
parentNode
Contains the parent node. Read-only.
parsed
*Indicates the parsed status of the node and child nodes. Read-only.
prefix
*Returns the namespace prefix. Read-only.
previousSibling
Contains the previous sibling of this node in the parent‘s child list. Read-only.
specified
Indicates whether the node (usually an attribute) is explicitly specified or derived from a default value in the document type definition (DTD) or schema. Read-only.
text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write.
value
Contains the attribute value. Read/write.
xml
Contains the XML representation of the node and all its descendants. Read-only.
方法:
appendChild
Appends new child node as the last child of this node.
cloneNode
Clones a new node.
hasChildNodes
Provides a fast way to determine whether a node has children.
insertBefore
Inserts a child node to the left of the specified node or at the end of the list.
removeChild
Removes the specified child node from the list of children and returns it.
replaceChild
Replaces the specified old child node with the supplied new child node.
selectNodes
Applies the specified pattern-matching operation to this node‘s context and returns the list of matching nodes as IXMLDOMNodeList.
selectSingleNode
Applies the specified pattern-matching operation to this node‘s context and returns the first matching node.
transformNode
Processes this node and its children using the supplied XSL Transformations (XSLT) style sheet and returns the resulting transformation.
transformNodeToObject
Processes this node and its children using the supplied XSL Transformations (XSLT) style sheet and returns the resulting transformation in the supplied object.
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。