XMLDOM是用來(lái)訪問(wèn)和操作XML文檔的編程接口規(guī)范。
1、簡(jiǎn)介
XMLDOM被設(shè)計(jì)為可用于任何語(yǔ)言和任何操作系統(tǒng)。借助DOM,程序員可以創(chuàng)建XML文檔、遍歷其結(jié)構(gòu),增、改、刪其元素。DOM將整個(gè)XML文檔視作一棵樹(shù),文檔級(jí)的元素是樹(shù)的根。
2、MS的XML解析,IE5.0以上。
是一個(gè)COM組件,至少包含下列對(duì)象:
(1)Micosoft.XMLDOM
(2)Micosoft.XMLDOM.parseError,有如下屬性:
Property |
Description |
errorCode |
Returns a long integer error code |
reason |
Returns a string explaining the reason for the error |
line |
Returns a long integer representing the line number for the error |
linePos |
Returns a long integer representing the line position for the error |
srcText |
Returns a string containing the line that caused the error |
url |
Returns the url pointing the loaded document |
filePos |
Returns a long integer file position of the error |
(3)Microsoft.XMLHTTP,有如下屬性:
Property |
Description |
readyState |
Returns the state of the document |
responseBody |
Returns the response as an array of unsigned bytes |
responseStream |
Returns the response as an IStream |
responseText |
Returns the response as a string |
responseXML |
Returns the response as an xml document |
status |
Returns the status code as a number |
statusText |
Returns the status as a string |
有如下方法:
Property |
Description |
abort() |
Cancel the current http request |
getAllResponseHeaders() |
Returns the value of the http headers |
getResponseHeader(headerName) |
Returns the value of one specified http header |
open(method, url, async, userid, password) |
Opens http request, and specifies the information |
send() |
Send the http request to the server |
setRequestHeader(headerName,headerValue) |
Specifies the name of a http header |
(4)Node的類型
nodeType |
Named Constant |
nodeTypeString |
nodeName |
nodeValue |
1 |
ELEMENT_NODE |
element |
tagName |
null |
2 |
ATTRIBUTE_NODE |
attribute |
name |
value |
3 |
TEXT_NODE |
text |
#text |
content of node |
4 |
CDATA_SECTION_NODE |
cdatasection |
#cdata-section |
content of node |
5 |
ENTITY_REFERENCE_NODE |
entityreference |
entity reference name |
null |
6 |
ENTITY_NODE |
entity |
entity name |
null |
7 |
PROCESSING_INSTRUCTION_NODE |
processinginstruction |
target |
content of node |
8 |
COMMENT_NODE |
comment |
#comment |
comment text |
9 |
DOCUMENT_NODE |
document |
#document |
null |
10 |
DOCUMENT_TYPE_NODE |
documenttype |
doctype name |
null |
11 |
DOCUMENT_FRAGMENT_NODE |
documentfragment |
#document fragment |
null |
12 |
NOTATION_NODE |
notation |
notation name |
null |
W3C規(guī)定的屬性:
Property |
Description |
attributes |
Returns a NamedNodeMap containing all attributes for this node |
childNodes |
Returns a NodeList containing all the child nodes for this node |
firstChild |
Returns the first child node for this node |
lastChild |
Returns the last child node for this node |
nextSibling |
Returns the next sibling node. Two nodes are siblings if they have the same parent node |
nodeName |
Returns the nodeName, depending on the type |
nodeType |
Returns the nodeType as a number |
nodeValue |
Returns, or sets, the value of this node, depending on the type |
ownerDocument |
Returns the root node of the document |
parentNode |
Returns the parent node for this node |
previousSibling |
Returns the previous sibling node. Two nodes are siblings if they have the same parent node |
W3C規(guī)定的方法:
Method |
Description |
appendChild(newChild) |
Appends the node newChild at the end of the child nodes for this node |
cloneNode(boolean) |
Returns an exact clone of this node. If the boolean value is set to true, the cloned node contains all the child nodes as well |
hasChildNodes() |
Returns true if this node has any child nodes |
insertBefore(newNode,refNode) |
Inserts a new node, newNode, before the existing node, refNode |
removeChild(nodeName) |
Removes the specified node, nodeName |
replaceChild(newNode,oldNode) |
Replaces the oldNode, with the newNode |
(5)NodeList的W3C規(guī)定的屬性和方法。
Property |
Description |
length |
Returns the number of nodes in a nodeList |
Method |
Description |
item |
Returns a specific node in the nodeList |
XMLHTTP對(duì)象及其方法
------------------
MSXML中提供了Microsoft.XMLHTTP對(duì)象,能夠完成從數(shù)據(jù)包到Request對(duì)象的轉(zhuǎn)換以及發(fā)送任務(wù)。
創(chuàng)建XMLHTTP對(duì)象的語(yǔ)句如下:
Set objXML = CreateObject("Msxml2.XMLHTTP") 或
Set objXML = CreateObject(“Microsoft.XMLHTTP”)
' Or, for version 3.0 of XMLHTTP, use:
' Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
對(duì)象創(chuàng)建后調(diào)用Open方法對(duì)Request對(duì)象進(jìn)行初始化,語(yǔ)法格式為:
poster.open http-method, url, async, userID, password
Open方法中包含了5個(gè)參數(shù),前三個(gè)是必要的,后兩個(gè)是可選的(在服務(wù)器需要進(jìn)行身份驗(yàn)證時(shí)提供)。參數(shù)的含義如下所示:
http-method: HTTP的通信方式,比如GET或是 POST
url: 接收XML數(shù)據(jù)的服務(wù)器的URL地址。通常在URL中要指明 ASP或CGI程序
async: 一個(gè)布爾標(biāo)識(shí),說(shuō)明請(qǐng)求是否為異步的。如果是異步通信方式(true),客戶機(jī)就不等待服務(wù)器的響應(yīng);如果是同步方式(false),客戶機(jī)就要等到服務(wù)器返回消息后才去執(zhí)行其他操作
userID 用戶ID,用于服務(wù)器身份驗(yàn)證
password 用戶密碼,用于服務(wù)器身份驗(yàn)證
XMLHTTP對(duì)象的Send方法
用Open方法對(duì)Request對(duì)象進(jìn)行初始化后,調(diào)用Send方法發(fā)送XML數(shù)據(jù):
poster.send XML-data
Send方法的參數(shù)類型是Variant,可以是字符串、DOM樹(shù)或任意數(shù)據(jù)流。發(fā)送數(shù)據(jù)的方式分為同步和異步兩種。在異步方式下,數(shù)據(jù)包一旦發(fā)送完畢,就結(jié)束Send進(jìn)程,客戶機(jī)執(zhí)行其他的操作;而在同步方式下,客戶機(jī)要等到服務(wù)器返回確認(rèn)消息后才結(jié)束Send進(jìn)程。
XMLHTTP對(duì)象中的readyState屬性能夠反映出服務(wù)器在處理請(qǐng)求時(shí)的進(jìn)展?fàn)顩r。客戶機(jī)的程序可以根據(jù)這個(gè)狀態(tài)信息設(shè)置相應(yīng)的事件處理方法。屬性值及其含義如下表所示:
值 說(shuō)明
0 Response對(duì)象已經(jīng)創(chuàng)建,但XML文檔上載過(guò)程尚未結(jié)束
1 XML文檔已經(jīng)裝載完畢
2 XML文檔已經(jīng)裝載完畢,正在處理中
3 部分XML文檔已經(jīng)解析
4 文檔已經(jīng)解析完畢,客戶端可以接受返回消息
客戶機(jī)處理響應(yīng)信息
客戶機(jī)接收到返回消息后,進(jìn)行簡(jiǎn)單的處理,基本上就完成了C/S之間的一個(gè)交互周期。客戶機(jī)接收響應(yīng)是通過(guò)XMLHTTP對(duì)象的屬性實(shí)現(xiàn)的:
● responseTxt:將返回消息作為文本字符串;
● responseXML:將返回消息視為XML文檔,在服務(wù)器響應(yīng)消息中含有XML數(shù)據(jù)時(shí)使用;
● responseStream:將返回消息視為Stream對(duì)象。
下面的xml文件是動(dòng)態(tài)生成的最后用xmlHTTP傳送出去,這是一個(gè)在客戶端JavaScript腳本里的內(nèi)容,當(dāng)然你也可以寫(xiě)在服務(wù)器,但是要相應(yīng)的改一些東西:(僅供大家參考,了解它的用法)
var xmlDoc=new ActiveXObject("MSXML2.DOMDocument");
flag=xmlDoc.loadXML("");
newNode =xmlDoc.createElement("編碼")
MarkNode=xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartMark")
newNode.text=StartMark;
MarkNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndMark")
newNode.text=EndMark;
MarkNode.appendChild(newNode)
newNode =xmlDoc.createElement("日期")
DateNode=xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartDate");
newNode.text=StartDate;
DateNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndDate")
newNode.text=EndDate;
DateNode.appendChild(newNode);
newNode =xmlDoc.createElement("數(shù)量")
SLNode =xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartSL")
newNode.text=StartShuL
SLNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndSL");
newNode.text=EndShuL
SLNode.appendChild(newNode);
newNode =xmlDoc.createElement("單價(jià)")
DJNode =xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("StartDJ")
newNode.text=StartDanJ;
DJNode.appendChild(newNode);
newNode =xmlDoc.createElement("EndDJ")
newNode.text=EndDanJ;
DJNode.appendChild(newNode);
newNode =xmlDoc.createElement("金額")
JENode =xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("StartJE")
newNode.text=StartJinE
JENode.appendChild(newNode)
newNode =xmlDoc.createElement("EndJE")
newNode.text=EndJinE
JENode.appendChild(newNode)
newNode =xmlDoc.createElement("倉(cāng)庫(kù)代碼")
newNode.text=CK;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("票號(hào)")
newNode.text=RKPH;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("單位代碼")
newNode.text=CorpName;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("BiaoShi")
newNode.text=Biaoshi
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("FindCate")
newNode.text=FindCate
xmlDoc.documentElement.appendChild(newNode)
var xh =new ActiveXObject("MSXML2.XMLHTTP")
xh.open("POST","Find.asp",false)
xh.setRequestHeader("Content-Type","text/xml")
xh.setRequestHeader("Content-Type","gb2312")
xh.send(xmlDoc);
我的每一個(gè)newNode的text值是一個(gè)變量,也就是我客戶端form 中input的值
該文章在 2011/4/13 14:52:24 編輯過(guò)