XML認證教程:DOM Parser

才智咖 人氣:6.67K

DOM= Document Object Model,文件物件模型,DOM可以以一種獨立於平臺和語言的方式訪問和修改一個文件的內容和結構。

DOM基礎

在開始使用 DOM 之前,瞭解它實際表示什麼是值得的。DOM Document 是以層次結構組織起來的節點,或資訊片段,的集合。這種層次結構允許開發 者瀏覽樹來查詢特定資訊。通常,分析結構需要在完成任何工作之前裝入整個 文件並且裝入層次結構。

由於 DOM 是基於資訊的層次結構,因此它被稱為是基於樹的。

對於極其大的文件,裝入整個文件並對該文件進行解析會很慢且佔用大量資源, 所以要用其它方式來處理資料。一些基於事件的模型,如 Simple API for XML(SAX),是工作在資料流之上,在資料流經過時對其進行處理。基於事件 的 API 消除了在記憶體中構建資料樹的需要,但它不允許開發者實際更改原始 文件中的資料。

另一方面,DOM 還提供了一個 API,該 API 允許開發者為建立應用程式而在樹的任何地方新增、編輯、移動或除去節點。

DOM 基本的節點型別

XML 中最常見的節點型別:

Node: DOM 基本的資料型別。

Element: 您將最主要處理的物件是 Element。

Attr: 代表一個元素的屬性。

Text: 一個 Element 或 Attr 的實際內容。

Document: 代表整個 XML 文件。一個 Document 物件通常也被稱為一棵 DOM 樹。

較不常見的節點型別:CData、註釋、處理指令和文件片段:

CData:“字元資料”的縮寫註釋:註釋包含有關資料的資訊,通常應用程式會忽略它們。處理指令:PI 是專門針對應用程式的資訊。文件片段:為了形成良好的格式,文件只能有一個根元素。有時,必須臨時建立幾組元素,這些元素不是滿足需求所必要的。文件片段類似於這樣:

Silver Show Saddle, 16 inch

825.00

1

Premium Cinch

49.00

1

解析文件的三步過程

為了使用 XML 檔案中的資訊,必須解析該檔案以建立 Document 物件。

Document 物件是一個介面,所以不能直接例項化;相反,應用程式一般使用 factory。確切的過程隨實現的不同而不同,但想法是相同的。在示例 JAXP 環境中,解析檔案是一個三步過程:

建立 DocumentBuilderFactory。該物件將建立 DocumentBuilder。

建立 DocumentBuilder。 DocumentBuilder 將實際進行解析以建立 Document 物件。

解析該檔案以建立 Document 物件。

如果需要,在不必更改程式碼的情況下,JAXP 允許插進不同的解析器。讓我們繼續,開始構建應用程式。

基本的應用程式

從建立基本的應用程式,名為 OrderProcessor 的類開始。

import mentBuilder;

import mentBuilderFactory;

import ;

import ment;

public class OrderProcessor {

public static void main (String args[]) {

File docFile = new File("");

Document doc = null;

try {

DocumentBuilderFactory dbf = nstance();

DocumentBuilder db = ocumentBuilder();

doc = e(docFile);

} catch (Exception e) {

t("Problem parsing the file.");

}

}

}

首先,Java 匯入必要的類,然後建立 OrderProcessor 應用程式。在本教程中的這個示例將只處理一個檔案,所以為簡短起見,該應用程式包含對它的直接引用。

應用程式在 try-catch 塊外部定義了 Document 物件,以便在後面使用該物件。try-catch 使您能執行可能會丟擲異常的一些操作,這樣不會危及整個應用程式。如果異常丟擲,則應用程式簡單地執行相應的. catch 程式碼。

在 try-catch 塊內部,應用程式建立 DocumentBuilderFactory,然後使用它來建立 DocumentBuilder。最後,DocumentBuilder 解析該檔案以建立 Document。

DOM 常用方法

ocumentElement()

返回文件的根(root)元素。

irstChild() and astChild()

返回給定 Node 的第一個子女。

extSibling() and reviousSibling()

這些方法返回下一個或前一個給定 Node 的同胞。

ttribute(attrName)

對給定的 Node,返回給定名稱的屬性。例如,如果您要獲得名為 id 屬性 的物件,可呼叫 getAttribute("id")。

編輯文件

更改節點資料

odeValue(elemValue);

新增節點

String totalString = new Double(total)ring();

Node totalNode = teTextNode(totalString);

//Document 物件建立新的文字節點,該節點帶有作為值的 totalString

Element totalElement = teElement("total");

//建立新元素 total

ndChild(totalNode);

// 將節點新增到新的 total 元素。

rtBefore(totalElement, irstChild());

//將新元素新增到 Document,指定新的 Node,然後指定新 Node 在 Node 之前

除去節點

Node deadNode = arentNode()veChild(thisOrderItem);

替換節點

Element backElement = teElement("backordered");

//建立新元素 backordered

Node deadNode =

arentNode()aceChild(backElement,thisOrderItem);

建立和設定屬性

Element backElement = teElement("backordered");

//建立新元素 backordered

ttributeNode(teAttribute("itemid"));

//建立新屬性 itemid

String itemIdString = ttributeNode("itemid")odeValue();

//取得thisOrderItem的屬性itemid的值

ttribute("itemid", itemIdString);

//設定backElement的屬性item的值,可以省略createAttribute

Node deadNode =

arentNode()aceChild(backElement,thisOrderItem);

除去屬性

Element thisOrder = (Element)(orderNum);

Element customer = (Element)lementsByTagName("cusomertid")(0);

veAttribute("limit");

//去除屬性limit

TAGS:DOM Parser XML