xmldom 手册

    技术2022-05-11  72

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

      1、简介  XMLDOM被设计为可用于任何语言和任何操作系统。借助DOM,程序员可以创建XML文档、遍历其结构,增、改、删其元素。DOM将整个XML文档视作一棵树,文档级的元素是树的根。

      2、MS的XML解析,IE5.0以上。  是一个COM组件,至少包含下列对象:  (1)Micosoft.XMLDOM  (2)Micosoft.XMLDOM.parseError,有如下属性:

    PropertyDescriptionerrorCodeReturns a long integer error codereasonReturns a string explaining the reason for the errorlineReturns a long integer representing the line number for the errorlinePosReturns a long integer representing the line position for the errorsrcTextReturns a string containing the line that caused the errorurlReturns the url pointing the loaded documentfilePosReturns a long integer file position of the error   (3)Microsoft.XMLHTTP,有如下属性: PropertyDescriptionreadyStateReturns the state of the documentresponseBodyReturns the response as an array of unsigned bytesresponseStreamReturns the response as an IStreamresponseTextReturns the response as a stringresponseXMLReturns the response as an xml documentstatusReturns the status code as a numberstatusTextReturns the status as a string 有如下方法: PropertyDescriptionabort()Cancel the current http requestgetAllResponseHeaders()Returns the value of the http headersgetResponseHeader(headerName)Returns the value of one specified http headeropen(method, url, async, userid, password)Opens http request, and specifies the informationsend()Send the http request to the serversetRequestHeader(headerName,headerValue)Specifies the name of a http header   (4)Node的类型 nodeTypeNamed ConstantnodeTypeStringnodeNamenodeValue1ELEMENT_NODEelementtagNamenull2ATTRIBUTE_NODEattributenamevalue3TEXT_NODEtext#textcontent of node4CDATA_SECTION_NODEcdatasection#cdata-sectioncontent of node5ENTITY_REFERENCE_NODEentityreferenceentity reference namenull6ENTITY_NODEentityentity namenull7PROCESSING_INSTRUCTION_NODEprocessinginstructiontargetcontent of node8COMMENT_NODEcomment#commentcomment text9DOCUMENT_NODEdocument#documentnull10DOCUMENT_TYPE_NODEdocumenttypedoctype namenull11DOCUMENT_FRAGMENT_NODEdocumentfragment#document fragmentnull12NOTATION_NODEnotationnotation namenull   W3C规定的属性: PropertyDescriptionattributesReturns a NamedNodeMap containing all attributes for this nodechildNodesReturns a NodeList containing all the child nodes for this nodefirstChildReturns the first child node for this nodelastChildReturns the last child node for this nodenextSiblingReturns the next sibling node. Two nodes are siblings if they have the same parent nodenodeNameReturns the nodeName, depending on the typenodeTypeReturns the nodeType as a numbernodeValueReturns, or sets, the value of this node, depending on the typeownerDocumentReturns the root node of the documentparentNodeReturns the parent node for this nodepreviousSiblingReturns the previous sibling node. Two nodes are siblings if they have the same parent node   W3C规定的方法: MethodDescriptionappendChild(newChild)Appends the node newChild at the end of the child nodes for this nodecloneNode(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 wellhasChildNodes()Returns true if this node has any child nodesinsertBefore(newNode,refNode)Inserts a new node, newNode, before the existing node, refNoderemoveChild(nodeName)Removes the specified node, nodeNamereplaceChild(newNode,oldNode)Replaces the oldNode, with the newNode

     

      (5)NodeList的W3C规定的属性和方法。

    PropertyDescriptionlengthReturns the number of nodes in a nodeList MethodDescriptionitemReturns a specific node in the nodeList

    XMLHTTP对象及其方法------------------MSXML中提供了Microsoft.XMLHTTP对象,能够完成从数据包到Request对象的转换以及发送任务。 创建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")对象创建后调用Open方法对Request对象进行初始化,语法格式为: poster.open http-method, url, async, userID, password Open方法中包含了5个参数,前三个是必要的,后两个是可选的(在服务器需要进行身份验证时提供)。参数的含义如下所示:  http-method: HTTP的通信方式,比如GET或是 POST url: 接收XML数据的服务器的URL地址。通常在URL中要指明 ASP或CGI程序 async: 一个布尔标识,说明请求是否为异步的。如果是异步通信方式(true),客户机就不等待服务器的响应;如果是同步方式(false),客户机就要等到服务器返回消息后才去执行其他操作 userID 用户ID,用于服务器身份验证 password 用户密码,用于服务器身份验证 XMLHTTP对象的Send方法 用Open方法对Request对象进行初始化后,调用Send方法发送XML数据: poster.send XML-data Send方法的参数类型是Variant,可以是字符串、DOM树或任意数据流。发送数据的方式分为同步和异步两种。在异步方式下,数据包一旦发送完毕,就结束Send进程,客户机执行其他的操作;而在同步方式下,客户机要等到服务器返回确认消息后才结束Send进程。 XMLHTTP对象中的readyState属性能够反映出服务器在处理请求时的进展状况。客户机的程序可以根据这个状态信息设置相应的事件处理方法。属性值及其含义如下表所示: 值 说明 0 Response对象已经创建,但XML文档上载过程尚未结束 1 XML文档已经装载完毕 2 XML文档已经装载完毕,正在处理中 3 部分XML文档已经解析 4 文档已经解析完毕,客户端可以接受返回消息客户机处理响应信息 客户机接收到返回消息后,进行简单的处理,基本上就完成了C/S之间的一个交互周期。客户机接收响应是通过XMLHTTP对象的属性实现的: ● responseTxt:将返回消息作为文本字符串; ● responseXML:将返回消息视为XML文档,在服务器响应消息中含有XML数据时使用; ● responseStream:将返回消息视为Stream对象。 

    下面的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的值

     

    最新回复(0)