XMLDOM是用來訪問和操作XML文檔的編程接口規(guī)范。
1、簡介
XMLDOM被設計為可用于任何語言和任何操作系統(tǒng)。借助DOM,程序員可以創(chuàng)建XML文檔、遍歷其結構,增、改、刪其元素。DOM將整個XML文檔視作一棵樹,文檔級的元素是樹的根。
2、MS的XML解析,IE5.0以上。
是一個COM組件,至少包含下列對象:
?。?)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 |
?。?)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 |
?。?)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 |
?。?)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對象及其方法
------------------
MSXML中提供了Microsoft.XMLHTTP對象,能夠完成從數(shù)據(jù)包到Request對象的轉換以及發(fā)送任務。
創(chuàng)建XMLHTTP對象的語句如下:
Set objXML = CreateObject("Msxml2.XMLHTTP") 或
Set objXML = CreateObject(“Microsoft.XMLHTTP”)
' Or, for version 3.0 of XMLHTTP, use:
' Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
對象創(chuàng)建后調用Open方法對Request對象進行初始化,語法格式為:
poster.open http-method, url, async, userID, password
Open方法中包含了5個參數(shù),前三個是必要的,后兩個是可選的(在服務器需要進行身份驗證時提供)。參數(shù)的含義如下所示:
http-method: HTTP的通信方式,比如GET或是 POST
url: 接收XML數(shù)據(jù)的服務器的URL地址。通常在URL中要指明 ASP或CGI程序
async: 一個布爾標識,說明請求是否為異步的。如果是異步通信方式(true),客戶機就不等待服務器的響應;如果是同步方式(false),客戶機就要等到服務器返回消息后才去執(zhí)行其他操作
userID 用戶ID,用于服務器身份驗證
password 用戶密碼,用于服務器身份驗證
XMLHTTP對象的Send方法
用Open方法對Request對象進行初始化后,調用Send方法發(fā)送XML數(shù)據(jù):
poster.send XML-data
Send方法的參數(shù)類型是Variant,可以是字符串、DOM樹或任意數(shù)據(jù)流。發(fā)送數(shù)據(jù)的方式分為同步和異步兩種。在異步方式下,數(shù)據(jù)包一旦發(fā)送完畢,就結束Send進程,客戶機執(zhí)行其他的操作;而在同步方式下,客戶機要等到服務器返回確認消息后才結束Send進程。
XMLHTTP對象中的readyState屬性能夠反映出服務器在處理請求時的進展狀況??蛻魴C的程序可以根據(jù)這個狀態(tài)信息設置相應的事件處理方法。屬性值及其含義如下表所示:
值 說明
0 Response對象已經(jīng)創(chuàng)建,但XML文檔上載過程尚未結束
1 XML文檔已經(jīng)裝載完畢
2 XML文檔已經(jīng)裝載完畢,正在處理中
3 部分XML文檔已經(jīng)解析
4 文檔已經(jīng)解析完畢,客戶端可以接受返回消息
客戶機處理響應信息
客戶機接收到返回消息后,進行簡單的處理,基本上就完成了C/S之間的一個交互周期。客戶機接收響應是通過XMLHTTP對象的屬性實現(xiàn)的:
● responseTxt:將返回消息作為文本字符串;
● responseXML:將返回消息視為XML文檔,在服務器響應消息中含有XML數(shù)據(jù)時使用;
● responseStream:將返回消息視為Stream對象。
下面的xml文件是動態(tài)生成的最后用xmlHTTP傳送出去,這是一個在客戶端JavaScript腳本里的內容,當然你也可以寫在服務器,但是要相應的改一些東西:(僅供大家參考,了解它的用法)
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("單價")
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("倉庫代碼")
newNode.text=CK;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("票號")
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);
我的每一個newNode的text值是一個變量,也就是我客戶端form 中input的值
該文章在 2011/4/13 14:52:24 編輯過