随笔-60  评论-98  文章-0  trackbacks-0
Part1:对节点的操作

重命名节点、合并内存中的两个文档或者将一个文档中的部分内容加入到另一个文档中

1// Renaming nodes
2Element element = document.createElementNS("http://example.com""street");
3// if implementation can rename the node, element returned
4// is the same object as was originally created
5element = document.renameNode(element, "http://example.com""address");
6// adopting previously created node to a different document
7Node adoptedNode = document2.adoptNode(element);
8

比较节点——测试两个节点是否相等、是否相同以及它们在文档资料树中的相对位置
两个对象要相同,它们必须在内存中是 同一个对象。另一方面,两个对象要相等,它们只需具有相同的特性即可。因此,两个相同的对象必定是相等的,但是两个相等的对象不一定是相同的。
isEqualNode  // 判等
isSameNode  // 判同
compareDocumentPosition  // 一个节点是另一个节点的后代还是祖先、在前面还是后面等等

处理文本——检索和设置一个 Element 节点的文本内容、wholeText
1String oldContent = elem.getTextContent();
2elem.setTextContent("content");
3
Text 接口的新属性 wholeText 它返回在逻辑相邻的文本节点中所包含的所有文本

使用数据
当附加了一些数据的节点上有事件发生时,已注册的处理程序将被调用,并提供所有必需的信息来相应地更新结构

Part2:对文档的操作、访问类型信息和Xerces中的实现方式
映射到 Infoset——新的 Appendix C 提供了 XML Infoset 模型与 DOM 之间的映射
 1// XML Declaration information on
 2// the org.w3c.dom.Document interface
 3public String getXmlEncoding(); //获取变法方式
 4public void setXmlEncoding(String xmlEncoding); // 设置编码方式
 5public boolean getXmlStandalone();
 6public void setXmlStandalone(boolean xmlStandalone)
 7                                  throws DOMException;
 8public String getXmlVersion(); // xml版本
 9public void setXmlVersion(String xmlVersion)
10                                  throws DOMException;
11// element content whitespace property on the Text 
12// interface
13public boolean isWhitespaceInElementContent(); // 一个 Text 节点是否只包含可以被忽略的空白
14
通过 Attr 接口的 schemaTypeInfo 属性,您还可以获取一个属性信息项的属性类型特性的值 ——即一个属性的类型。
在这种映射中,XML Infoset 信息项都映射到其相应的 Node ,反之也一样,一个信息项的每一个属性都映射到其相应 Node 的属性

自举——DOMImplementationRegistry 对象,通过使用机制机制,就可以使用对于应用程序最合适的实现
1// set DOMImplementationRegistry.PROPERTY property 
2// to reference all known DOM implementations
3System.setProperty(DOMImplementationRegistry.PROPERTY,
4                   "org.apache.xerces.dom.DOMImplementationSourceImpl");
5// get an instance of DOMImplementationRegistry
6DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
7// DOM implementation that support the specified features
8DOMImplementation i = registry.getDOMImplementation("MutationEvent");
9

文档标准化——Document 接口的 normalizeDocument 方法
通过 DOMConfiguration 配置 normalizeDocument ,以便对文档执行其他操作
 1// retrieve document configuration
 2DOMConfiguration config = document.getConfig();
 3// remove comments from
 4config.setParameter("comments"false);
 5// remove namespace declarations
 6config.setParameter("namespace-declarations"false);
 7// transform document
 8core.normalizeDocument();
 9// put document into a form closest to the XML Infoset 
10config.setParameter("infoset"true);
11// transform document
12core.normalizeDocument();
13
normalizeDocument 方法还允许您用 XML Schema 或者 DTD 对内存中的文档进行重新验证
为此,首先需要将 DOMConfiguration validate 参数设置为 true 。然后需要实现一个 DOMErrorHandler 对象,验证错误将报告给这个对象,再用 error-handler 参数将这个对象注册到 Document 上。这与对 SAX 解析器所做的工作很类似。最后,可以通过调用 normalizeDocument 检查文档是否有效。

访问类型信息——名为 TypeInfo 的新接口
如果使用 DTD (在装载的时候,或者使用 normalizeDocument ),那么一个属性节点的 TypeInfo 表示这个属性的类型。在 XML Infoset 中这是属性信息项的属性类型属性。不过,对于元素节点, TypeInfo 的名称为 null ,命名空间 URI 为 null ,这是因为 DTD 没有定义元素类型。
如果使用 XML Schema 验证文档,那么 TypeInfo 在元素节点上表示元素类型,而在属性节点上表示属性类型。

在 Xerces2 中使用 DOM Level 3 API
 1// Retrieve configuration
 2DOMConfiguration config = document.getConfig();
 3// Set document base URI
 4document.setDocumentURI("file:///c:/data");
 5// Configure the normalizeDocument operation
 6config.setParameter("schema-type""http://www.w3.org/2001/XMLSchema");
 7config.setParameter("validate"true);
 8config.setParameter("schema-location""personal.xsd");
 9// Revalidate your document in memory
10document.normalizeDocument();
11
验证内存中的文档
posted on 2009-06-12 18:28 创建更好的解决方案 阅读(1472) 评论(0)  编辑 收藏 引用 所属分类: 面向对象软件设计

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理