1、源码网资料下载:下载源码就到源码网,第八章 编程接口编程接口指应用程序访问 XML 文档的 API,常用的有 DOM、SAX 和 JDOM 三种,及微软的 MSXML 和.NET 的 Xml、Java 的 JAXP 三个。本章先概述 XML 之 API 的基本情况,然后分别介绍这三种和三个 API 的主要内容、编程方法和应用实例。一、概述1API1)需要为了开发 XML 应用程序,必须分析 XML 文档(包括识别元素标签、属性、注释和指令以及解析实体) ,以访问文档的各个语法成分和数据(如元素、属性、文本、注释和处理指令等) 。( 源码网整理: )为了避免低层次重复开发,需要有现成的语法分析器
2、及其动态链接库 DLL 作编程接口。为了防止各公司开发的分析器互不兼容,造成程序不通用,需要制定统一的接口规范(似ODBC/JDBC 于数据库) 。所以就有了 W3C 的 DOM、XML-DEV 邮件列表成员的 SAX 和Hunterreadonly attribute DOMImplementation implementation;readonly attribute Element documentElement;Element createElement(in DOMString tagName) raises(DOMException);DocumentFragment create
3、DocumentFragment();Text createTextNode(in DOMString data);Comment createComment(in DOMString data);CDATASection createCDATASection(in DOMString data) raises(DOMException);ProcessingInstruction createProcessingInstruction(in DOMString target, in DOMString data)raises(DOMException);Attr createAttribut
4、e(in DOMString name) raises(DOMException);EntityReference createEntityReference(in DOMString name) raises(DOMException);NodeList getElementsByTagName(in DOMString tagname);2)Node 接口Node(节点 )接口代表文档树中的一个节点,有多种节点类型(DOM1 中定义了 12 种,参见 IDL 定义) ,用符号常量表示,如:ELEMENT_NODE、ATTRIBUTE_NODE、TEXT_NODE 等。节点接口比较复杂,有
5、12 个节点类型属性常量、11 个其他属性和 6 个方法,没有父接口。属性父接口方法源码网资料下载:下载源码就到源码网,常用属性: nodeType 节点类型可取值:ELEMENT_NODE 元素节点、ATTRIBUTE_NODE 属性节点、TEXT_NODE 文本节点、CDATA_SECTION_NODE 字符数据节节点、ENTITY _REFERENCE_NODE 实体引用节点、ENTITY_NODE 实体节点、PROCESSING _INSTRUCTION_NODE 处理指令节点、COMMENT_NODE 注释节点、DOCUMENT _NODE 文档节点(根节点) 、DOCUMENT_T
6、YPE_NODE 文档类型节点、DOCUMENT _FRAGMENT_NODE 文档片段节点、NOTATION_NODE 记号节点,共12 种节点类型 nodeName 节点名称如元素名、属性名等 nodeValue 节点值如属性值、文本串等 attributes 属性属性的无序集(NamedNodeMap 类型) parentNode 父节点单个节点 childNodes 子节点有序节点集(NodeList 类型) firstChild 首子节点父节点的第一个子节点 lastChild 尾子节点父节点的最后一个子节点 previousSibling 前一兄弟直接前驱兄弟节点 nextSibl
7、ing 下一兄弟直接后继兄弟节点下面是一个典型的节点接口(元素节点):父节点前兄节点 后弟节点当前节点nodeType=ELEMENT_NODEnodeName=”学生”nodeValue=nullhasChildNode()=TrueparentNodepreviousSibling nextSiblingNodeList NamedNodeMap attributeschildNodeslastChildfirstChild方法: hasChildNodes 有子节点判断当前节点是否有子节点 insertBefore 前插将节点插入当前节点的指定子节点之前 appendChild 添加子节
8、点将节点添加到当前节点的所有子节点之后 removeChild 移去子节点删除指定子节点 replaceChild 替换子节点用新节点代替指定老子节点 cloneNode 克隆节点复制当前节点,以产生一个无父节点的新节点下面是节点接口的 IDL 定义:interface Node / NodeType源码网资料下载:下载源码就到源码网,const unsigned short ELEMENT_NODE = 1;const unsigned short ATTRIBUTE_NODE = 2;const unsigned short TEXT_NODE = 3;const unsigned sho
9、rt CDATA_SECTION_NODE = 4;const unsigned short ENTITY_REFERENCE_NODE = 5;const unsigned short ENTITY_NODE = 6;const unsigned short PROCESSING_INSTRUCTION_NODE = 7;const unsigned short COMMENT_NODE = 8;const unsigned short DOCUMENT_NODE = 9;const unsigned short DOCUMENT_TYPE_NODE = 10;const unsigned
10、short DOCUMENT_FRAGMENT_NODE = 11;const unsigned short NOTATION_NODE = 12;/ Other attributesreadonly attribute DOMString nodeName;attribute DOMString nodeValue; / raises(DOMException) on setting and retrievalreadonly attribute unsigned short nodeType;readonly attribute Node parentNode;readonly attri
11、bute NodeList childNodes;readonly attribute Node firstChild;readonly attribute Node lastChild;readonly attribute Node previousSibling;readonly attribute Node nextSibling;readonly attribute NamedNodeMap attributes;readonly attribute Document ownerDocument;/ MethodsNode insertBefore(in Node newChild,
12、in Node refChild) raises(DOMException);Node replaceChild(in Node newChild, in Node oldChild) raises(DOMException);Node removeChild(in Node oldChild) raises(DOMException);Node appendChild(in Node newChild) raises(DOMException);boolean hasChildNodes();Node cloneNode(in boolean deep);3)NodeList 接口NodeL
13、ist(节点列表 )接口是有序节点集的一种抽象,它是动态的 (live)会随着文档的改变(如加/删/换节点)而自动改变。节点列表接口非常简单,只有一个属性和一个方法,没有父接口。下面是节点列表接口的 IDL 定义:interface NodeList readonly attribute unsigned long length; / 属性节点集中的节点个数Node item(in unsigned long index); / 方法获取指定索引 index (= 0 length-1)的节点项;源码网资料下载:下载源码就到源码网,4)NamedNodeMap 接口NamedNodeMap(命
14、名节点映射)接口代表无序节点集,主要用于属性节点(因为标记语言中一个元素的多个属性是无序的即属性的顺序是无关紧要的 ) ,也是动态的。命名节点映射接口也比较简单,只有一个属性和四个方法,也没有父接口。下面是其IDL 定义:interface NamedNodeMap readonly attribute unsigned long length; / 集合中的节点数Node getNamedItem(in DOMString name); / 返回指定名称的节点,无则返回 nullNode setNamedItem(in Node arg) raises(DOMException); / 将
15、arg 节点添加到节点集中,/ 若集中有同名的节点,则替代Node removeNamedItem(in DOMString name) raises(DOMException); / 删除指定名称的节点Node item(in unsigned long index); / 获取指定索引(0 length-1)的节点项;3DOM 接口汇总下面分门别类简单罗列 DOM Level 1 中的接口的名称和 IDL。1)基本接口 (Fundamental Interfaces)基本接口是每个 DOM 实现必须全部实现的接口,DOM1 的核心部分中共有 11 个基本接口和 1 个异常。(1) DOM
16、异常(DOMException)exception DOMException unsigned short code;/ ExceptionCodeconst unsigned short INDEX_SIZE_ERR = 1;const unsigned short DOMSTRING_SIZE_ERR = 2;const unsigned short HIERARCHY_REQUEST_ERR = 3;const unsigned short WRONG_DOCUMENT_ERR = 4;const unsigned short INVALID_CHARACTER_ERR = 5;cons
17、t unsigned short NO_DATA_ALLOWED_ERR = 6;const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7;const unsigned short NOT_FOUND_ERR = 8;const unsigned short NOT_SUPPORTED_ERR = 9;const unsigned short INUSE_ATTRIBUTE_ERR = 10;源码网资料下载:下载源码就到源码网,(2) DOM 实现接口(DOMImplementation)interface DOMImplementation b
18、oolean hasFeature(in DOMString feature, in DOMString version);(3) 文档片段接口 (DocumentFragment)interface DocumentFragment : Node ;(4) 文档接口 (Document)interface Document : Node readonly attribute DocumentType doctype;readonly attribute DOMImplementation implementation;readonly attribute Element documentEl
19、ement;Element createElement(in DOMString tagName) raises(DOMException);DocumentFragment createDocumentFragment();Text createTextNode(in DOMString data);Comment createComment(in DOMString data);CDATASection createCDATASection(in DOMString data) raises(DOMException);ProcessingInstruction createProcess
20、ingInstruction(in DOMString target, in DOMString data)raises(DOMException);Attr createAttribute(in DOMString name) raises(DOMException);EntityReference createEntityReference(in DOMString name) raises(DOMException);NodeList getElementsByTagName(in DOMString tagname);(5) 节点接口 (Node)interface Node / No
21、deTypeconst unsigned short ELEMENT_NODE = 1;const unsigned short ATTRIBUTE_NODE = 2;const unsigned short TEXT_NODE = 3;const unsigned short CDATA_SECTION_NODE = 4;const unsigned short ENTITY_REFERENCE_NODE = 5;const unsigned short ENTITY_NODE = 6;const unsigned short PROCESSING_INSTRUCTION_NODE = 7;
22、const unsigned short COMMENT_NODE = 8;const unsigned short DOCUMENT_NODE = 9;const unsigned short DOCUMENT_TYPE_NODE = 10;源码网资料下载:下载源码就到源码网,const unsigned short DOCUMENT_FRAGMENT_NODE = 11;const unsigned short NOTATION_NODE = 12;/ Other attributesreadonly attribute DOMString nodeName;attribute DOMSt
23、ring nodeValue; / raises(DOMException) on setting and retrievalreadonly attribute unsigned short nodeType;readonly attribute Node parentNode;readonly attribute NodeList childNodes;readonly attribute Node firstChild;readonly attribute Node lastChild;readonly attribute Node previousSibling;readonly at
24、tribute Node nextSibling;readonly attribute NamedNodeMap attributes;readonly attribute Document ownerDocument;/ MethodsNode insertBefore(in Node newChild, in Node refChild) raises(DOMException);Node replaceChild(in Node newChild, in Node oldChild) raises(DOMException);Node removeChild(in Node oldChi
25、ld) raises(DOMException);Node appendChild(in Node newChild) raises(DOMException);boolean hasChildNodes();Node cloneNode(in boolean deep);(6) 节点列表接口 (NodeList)interface NodeList Node item(in unsigned long index); readonly attribute unsigned long length; ;(7) 命名节点映射接口 (NamedNodeMap)interface NamedNode
26、Map Node getNamedItem(in DOMString name);Node setNamedItem(in Node arg) raises(DOMException);Node removeNamedItem(in DOMString name) raises(DOMException);Node item(in unsigned long index);readonly attribute unsigned long length;(8) 字符数据接口 (CharacterData)interface CharacterData : Node 源码网资料下载:下载源码就到源
27、码网,attribute DOMString data; / raises(DOMException) on setting DOMString substringData(in unsigned long offset, in unsigned long count) raises(DOMException);void appendData(in DOMString arg) raises(DOMException);void insertData(in unsigned long offset, in DOMString arg) raises(DOMException);void del
28、eteData(in unsigned long offset, in unsigned long count) raises(DOMException);void replaceData(in unsigned long offset, in unsigned long count, in DOMString arg) raises(DOMException);(9) 属性接口 (Attr)interface Attr : Node readonly attribute DOMString name;readonly attribute boolean specified;attribute
29、 DOMString value;(10) 元素接口 (Element)interface Element : Node readonly attribute DOMString tagName;DOMString getAttribute(in DOMString name);void setAttribute(in DOMString name, in DOMString value) raises(DOMException);void removeAttribute(in DOMString name) raises(DOMException);Attr getAttributeNode
30、(in DOMString name);Attr setAttributeNode(in Attr newAttr) raises(DOMException);Attr removeAttributeNode(in Attr oldAttr) raises(DOMException);NodeList getElementsByTagName(in DOMString name);void normalize();(11) 文本接口 (Text)interface Text : CharacterData Text splitText(in unsigned long offset) rais
31、es(DOMException);(12) 注释接口 (Comment)interface Comment : CharacterData ;源码网资料下载:下载源码就到源码网,2)扩展接口 (Extended Interfaces)扩展接口主要针对 XML 文档, HTML 的 DOM 不必实现。DOM1 的核心部分中共有6 个扩展接口。(1) 字符数据节 (CDATASection)interface CDATASection : Text ;(2) 文档类型接口 (DocumentType)interface DocumentType : Node readonly attribute
32、DOMString name;readonly attribute NamedNodeMap entities;readonly attribute NamedNodeMap notations;(3) 记号接口 (Notation)interface Notation : Node readonly attribute DOMString publicId;readonly attribute DOMString systemId;(4) 实体接口 (Entity)interface Entity : Node readonly attribute DOMString publicId;re
33、adonly attribute DOMString systemId;readonly attribute DOMString notationName;(5) 实体引用接口 (EntityReference)interface EntityReference : Node ;源码网资料下载:下载源码就到源码网,(6) 处理指令接口 (ProcessingInstruction)interface ProcessingInstruction : Node readonly attribute DOMString target;attribute DOMString data; / raise
34、s(DOMException) on setting;3)HTML 接口HTML 接口是在核心接口基础上的扩展,包括 56 个接口,主要是与 HTML 4.0 的元素对应的接口。(1) HTML 集合(HTMLCollection)interface HTMLCollection readonly attribute unsigned long length;Node item(in unsigned long index);Node namedItem(in DOMString name);(2) HTML 文档(HTMLDocument)interface HTMLDocument : D
35、ocument attribute DOMString title;readonly attribute DOMString referrer;readonly attribute DOMString domain;readonly attribute DOMString URL;attribute HTMLElement body;readonly attribute HTMLCollection images;readonly attribute HTMLCollection applets;readonly attribute HTMLCollection links;readonly
36、attribute HTMLCollection forms;readonly attribute HTMLCollection anchors;attribute DOMString cookie;void open();void close();void write(in DOMString text);void writeln(in DOMString text);Element getElementById(in DOMString elementId);NodeList getElementsByName(in DOMString elementName);源码网资料下载:下载源码就
37、到源码网,(3) HTML 元素(HTMLElement)interface HTMLElement : Element attribute DOMString id;attribute DOMString title;attribute DOMString lang;attribute DOMString dir;attribute DOMString className;(4) HTML 的 Html 元素(HTMLHtmlElement)interface HTMLHtmlElement : HTMLElement attribute DOMString version;(5) HTML
38、 头元素(HTMLHeadElement)interface HTMLHeadElement : HTMLElement attribute DOMString profile;(6) HTML 链接元素(HTMLLinkElement)interface HTMLLinkElement : HTMLElement attribute boolean disabled;attribute DOMString charset;attribute DOMString href;attribute DOMString hreflang;attribute DOMString media;attrib
39、ute DOMString rel;attribute DOMString rev;attribute DOMString target;attribute DOMString type;(7) HTML 标题元素(HTMLTitleElement)interface HTMLTitleElement : HTMLElement attribute DOMString text;源码网资料下载:下载源码就到源码网,(8) HTML 元元素(HTMLMetaElement)interface HTMLMetaElement : HTMLElement attribute DOMString co
40、ntent;attribute DOMString httpEquiv;attribute DOMString name;attribute DOMString scheme;(9) HTML 基元素(HTMLBaseElement)interface HTMLBaseElement : HTMLElement attribute DOMString href;attribute DOMString target;(10) HTML 是索引元素(HTMLIsIndexElement)interface HTMLIsIndexElement : HTMLElement readonly attr
41、ibute HTMLFormElement form;attribute DOMString prompt;(11) HTML 样式元素(HTMLStyleElement)interface HTMLStyleElement : HTMLElement attribute boolean disabled;attribute DOMString media;attribute DOMString type;(12) HTML 体元素(HTMLBodyElement)interface HTMLBodyElement : HTMLElement attribute DOMString aLink
42、;attribute DOMString background;attribute DOMString bgColor;attribute DOMString link;attribute DOMString text;attribute DOMString vLink;源码网资料下载:下载源码就到源码网,(13) HTML 表单元素(HTMLFormElement)interface HTMLFormElement : HTMLElement readonly attribute HTMLCollection elements;readonly attribute long length;a
43、ttribute DOMString name;attribute DOMString acceptCharset;attribute DOMString action;attribute DOMString enctype;attribute DOMString method;attribute DOMString target;void submit();void reset();(14) HTML 选择元素(HTMLSelectElement)interface HTMLSelectElement : HTMLElement readonly attribute DOMString ty
44、pe;attribute long selectedIndex;attribute DOMString value;readonly attribute long length;readonly attribute HTMLFormElement form;readonly attribute HTMLCollection options;attribute boolean disabled;attribute boolean multiple;attribute DOMString name;attribute long size;attribute long tabIndex;void a
45、dd(in HTMLElement element, in HTMLElement before);void remove(in long index);void blur();void focus();(15) HTML 选项组元素(HTMLOptGroupElement)interface HTMLOptGroupElement : HTMLElement attribute boolean disabled;attribute DOMString label;源码网资料下载:下载源码就到源码网,(16) HTML 选项元素(HTMLOptionElement)interface HTMLOptionElement : HTMLElement readonly attribute HTMLFormElement form;attribute boolean defaultSelected;readonly attribute DOMString text;attribute long index;attribute boolean disabled;attribute DOMString lab