XMLDOM 参考

Back to Documentation Index

XMLDOM 是用来访问和操作XML文档的编程接口规范。

  1. 简介
    XMLDOM 被设计为可用于任何语言和任何操作系统。借助 DOM,程序员可以创建 XML 文档、遍历其结构,增、改、删其元素。DOM 将整个 XML 文档视作一棵树,文档级的元素是树的根。
  2. MS 的 XML 解析,IE 5.0 以上是一个 COM 组件,至少包含下列对象:
    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规定的属性
      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规定的方法
      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 规定的属性和方法
      属性
      Property Description
      length Returns the number of nodes in a nodeList
      方法
      Method Description
      item Returns a specific node in the nodeList
  3. XMLHTTP 对象及其方法

    MSXML 中提供了 Microsoft.XMLHTTP 对象,能够完成从数据包到 Request 对象的转换以及发送任务。

    创建 XMLHTTP 对象的语句如下:

    对象创建后调用 Open 方法对 Request 对象进行初始化,语法格式为:

    poster.open http-method, url, async, userID, password

    Open方法中包含了5个参数,前三个是必要的,后两个是可选的(在服务器需要进行身份验证时提供)。参数的含义如下所示:

    XMLHTTP 对象的 Send 方法

    用 Open 方法对 Request 对象进行初始化后,调用 Send 方法发送 XML 数据:

    poster.send XML-data

    Send方法的参数类型是 Variant,可以是字符串、DOM 树或任意数据流。发送数据的方式分为同步和异步两种。在异步方式下,数据包一旦发送完毕,就结束 Send 进程,客户机执行其他的操作;而在同步方式下,客户机要等到服务器返回确认消息后才结束 Send 进程。

    XMLHTTP 对象中的 readyState 属性能够反映出服务器在处理请求时的进展状况。客户机的程序可以根据这个状态信息设置相应的事件处理方法。属性值及其含义如下表所示:

    属性
    Value Description
    0 Response 对象已经创建,但XML文档上载过程尚未结束
    1 XML 文档已经装载完毕
    2 XML 文档已经装载完毕,正在处理中
    3 部分 XML 文档已经解析
    4 文档已经解析完毕,客户端可以接受返回消息

    客户机处理响应信息

    客户机接收到返回消息后,进行简单的处理,基本上就完成了 C/S 之间的一个交互周期。客户机接收响应是通过 XMLHTTP 对象的属性实现的:

    下面的 XML 文件是动态生成的最后用 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("数量")
    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 的值