重命名節點、合并內存中的兩個文檔或者將一個文檔中的部分內容加入到另一個文檔中
1
// Renaming nodes
2
Element 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
5
element = document.renameNode(element, "http://example.com", "address");
6
// adopting previously created node to a different document
7
Node adoptedNode = document2.adoptNode(element);
8
// Renaming nodes2
Element element = document.createElementNS("http://example.com", "street");3
// if implementation can rename the node, element returned4
// is the same object as was originally created5
element = document.renameNode(element, "http://example.com", "address");6
// adopting previously created node to a different document7
Node adoptedNode = document2.adoptNode(element);8

比較節點——測試兩個節點是否相等、是否相同以及它們在文檔資料樹中的相對位置
兩個對象要相同,它們必須在內存中是 同一個對象。另一方面,兩個對象要相等,它們只需具有相同的特性即可。因此,兩個相同的對象必定是相等的,但是兩個相等的對象不一定是相同的。
isEqualNode // 判等
isSameNode // 判同
compareDocumentPosition // 一個節點是另一個節點的后代還是祖先、在前面還是后面等等
處理文本——檢索和設置一個
Element 節點的文本內容、wholeText1
String oldContent = elem.getTextContent();
2
elem.setTextContent("content");
3
Text
String oldContent = elem.getTextContent();2
elem.setTextContent("content");3

接口的新屬性 wholeText 它返回在邏輯相鄰的文本節點中所包含的所有文本使用數據
當附加了一些數據的節點上有事件發生時,已注冊的處理程序將被調用,并提供所有必需的信息來相應地更新結構
Part2:對文檔的操作、訪問類型信息和Xerces中的實現方式
映射到 Infoset——新的 Appendix C 提供了 XML Infoset 模型與 DOM 之間的映射
1
// XML Declaration information on
2
// the org.w3c.dom.Document interface
3
public String getXmlEncoding(); //獲取變法方式
4
public void setXmlEncoding(String xmlEncoding); // 設置編碼方式
5
public boolean getXmlStandalone();
6
public void setXmlStandalone(boolean xmlStandalone)
7
throws DOMException;
8
public String getXmlVersion(); // xml版本
9
public void setXmlVersion(String xmlVersion)
10
throws DOMException;
11
// element content whitespace property on the Text
12
// interface
13
public boolean isWhitespaceInElementContent(); // 一個
14
通過
// XML Declaration information on2
// the org.w3c.dom.Document interface3
public String getXmlEncoding(); //獲取變法方式4
public void setXmlEncoding(String xmlEncoding); // 設置編碼方式5
public boolean getXmlStandalone();6
public void setXmlStandalone(boolean xmlStandalone)7
throws DOMException;8
public String getXmlVersion(); // xml版本9
public void setXmlVersion(String xmlVersion)10
throws DOMException;11
// element content whitespace property on the Text 12
// interface13
public boolean isWhitespaceInElementContent(); // 一個 Text 節點是否只包含可以被忽略的空白14

Attr 接口的 schemaTypeInfo 屬性,您還可以獲取一個屬性信息項的屬性類型特性的值 ——即一個屬性的類型。在這種映射中,XML Infoset 信息項都映射到其相應的
Node ,反之也一樣,一個信息項的每一個屬性都映射到其相應 Node 的屬性自舉——DOMImplementationRegistry 對象,通過使用機制機制,就可以使用對于應用程序最合適的實現
1
// set DOMImplementationRegistry.PROPERTY property
2
// to reference all known DOM implementations
3
System.setProperty(DOMImplementationRegistry.PROPERTY,
4
"org.apache.xerces.dom.DOMImplementationSourceImpl");
5
// get an instance of DOMImplementationRegistry
6
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
7
// DOM implementation that support the specified features
8
DOMImplementation i = registry.getDOMImplementation("MutationEvent");
9
// set DOMImplementationRegistry.PROPERTY property 2
// to reference all known DOM implementations3
System.setProperty(DOMImplementationRegistry.PROPERTY,4
"org.apache.xerces.dom.DOMImplementationSourceImpl");5
// get an instance of DOMImplementationRegistry6
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();7
// DOM implementation that support the specified features8
DOMImplementation i = registry.getDOMImplementation("MutationEvent");9

文檔標準化——Document
接口的 normalizeDocument 方法通過
DOMConfiguration 配置 normalizeDocument ,以便對文檔執行其他操作 1
// retrieve document configuration
2
DOMConfiguration config = document.getConfig();
3
// remove comments from
4
config.setParameter("comments", false);
5
// remove namespace declarations
6
config.setParameter("namespace-declarations", false);
7
// transform document
8
core.normalizeDocument();
9
// put document into a form closest to the XML Infoset
10
config.setParameter("infoset", true);
11
// transform document
12
core.normalizeDocument();
13
normalizeDocument
// retrieve document configuration2
DOMConfiguration config = document.getConfig();3
// remove comments from4
config.setParameter("comments", false);5
// remove namespace declarations6
config.setParameter("namespace-declarations", false);7
// transform document8
core.normalizeDocument();9
// put document into a form closest to the XML Infoset 10
config.setParameter("infoset", true);11
// transform document12
core.normalizeDocument();13

方法還允許您用 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
2
DOMConfiguration config = document.getConfig();
3
// Set document base URI
4
document.setDocumentURI("file:///c:/data");
5
// Configure the normalizeDocument operation
6
config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
7
config.setParameter("validate", true);
8
config.setParameter("schema-location", "personal.xsd");
9
// Revalidate your document in memory
10
document.normalizeDocument();
11
驗證內存中的文檔
// Retrieve configuration2
DOMConfiguration config = document.getConfig();3
// Set document base URI4
document.setDocumentURI("file:///c:/data");5
// Configure the normalizeDocument operation6
config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");7
config.setParameter("validate", true);8
config.setParameter("schema-location", "personal.xsd");9
// Revalidate your document in memory10
document.normalizeDocument();11



