我覺得你所應該知道的Dom4J

創建解析器:

              SAXReader reader = new SAXReader();

利用解析器讀入xml文檔:
       Document   document = reader.read(new File("input.xml"));

獲取文檔的根節點:

Element root = document.getRootElement();

接口繼承結構:

Node        ---

Branch

--Document

--Element

----

Attribute

 

Node接口

String

asXML()
asXMLreturns the textual XML representation of this node.

將一個節點轉換爲字符串

String

getName()
getNamereturns the name of this node.

獲取節點的名稱,如果是元素則獲取到元素名,如果是屬性獲取到屬性名

short

getNodeType()
Returns the code according to the type of node.

獲取節點類型,在Node接口上定義了一些靜態short類型的常量用來表示各種類型

Element

getParent()
getParentreturns the parent Element if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.

獲取父節點,如果是根元素調用則返回null,如果是其他元素調用則返回父元素,如果是屬性調用則返回屬性所依附的元素。

String

getText()
Returns the text of this node.

返回節點文本,如果是元素則返回標籤體,如果是屬性則返回屬性值

List

selectNodes(String xpathExpression)
selectNodesevaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.

利用xpath表達式,選擇節點

void

setName(String name)
Sets the text data of this node or this method will throw an UnsupportedOperationException if it is read-only.

設置節點的名稱,元素可以更改名稱,屬性則不可以,會拋出UnsupportedOperationException 異常

void

setText(String text)
Sets the text data of this node or this method will throw an UnsupportedOperationException if it is read-only.

設置節點內容,如果是元素則設置標籤體,如果是屬性則設置屬性的值

void

write(Writer writer)
writewrites this node as the default XML notation for this node.

將節點寫出到一個輸出流中,元素、屬性均支持

 

 

Branch接口(實現了Node接口)

void

add(Element element)
Adds the given Element to this branch.

增加一個子節點

Element

addElement(QName qname)
Adds a new Element node with the given QNameto this branch and returns a reference to the new node.

增加一個給定名字的子節點,並且返回這個新創建的節點的引用

int

indexOf(Node node)
Returns the index of the given node if it is a child node of this branch or -1 if the given node is not a child node.

獲取給定節點在所有直接點中的位置號,如果該節點不是此分支的子節點,則返回-1

boolean

remove(Element element)
Removes the given Element if the node is an immediate child of this branch.

刪除給定子元素,返回布爾值表明是否刪除成功。

 

Element接口(實現了BranchNode接口)

void

add(Attribute attribute)
Adds the given Attribute to this element.

增加一個屬性

Element

addAttribute(QName qName, String value)
Adds the attribute value of the given fully qualified name.

爲元素增加屬性,用給定的屬性名和屬性值,並返回該元素

Element

addAttribute(String name, String value)
           Adds the attribute value of the given local name.

爲元素增加屬性

Attribute

attribute(int index)
Returns the attribute at the specified indexGets the

獲取指定位置的屬性

Attribute

attribute(QName qName)
DOCUMENT ME!

獲取指定名稱的屬性

Iterator

attributeIterator()
DOCUMENT ME!

獲取屬性迭代器

List

attributes()
Returns the Attributeinstances this element contains as a backed Listso that the attributes may be modified directly using the Listinterface.

獲取該元素的所有屬性,以一個list返回

String

attributeValue(QName qName)
This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

獲取指定名稱屬性的值,如果不存在該屬性返回null,如果存在該屬性但是屬性值爲空,則返回空字符串

Element

element(QName qName)
Returns the first element for the given fully qualified name.

獲取指定名稱的子元素,如果有多個該名稱的子元素,則返回第一個

Element

element(String name)
Returns the first element for the given fully qualified name.

獲取指定名稱的子元素,如果有多個該名稱的子元素,則返回第一個

Iterator

elementIterator()
Returns an iterator over all this elements child elements.

獲取子元素迭代器

Iterator

elementIterator(QName qName)
Returns an iterator over the elements contained in this element which match the given fully qualified name.

獲取指定名稱的子元素的迭代器

List

elements()
Returns the elements contained in this element.

獲取所有子元素,並用一個list返回

List

elements(QName qName)
Returns the elements contained in this element with the given fully qualified name.

獲取所有指定名稱的子元素,並用一個list返回

String

getText()
Returns the text value of this element without recursing through child elements.

獲取元素標籤體

boolean

remove(Attribute attribute)
Removes the given Attribute from this element.

移除元素上的屬性

void

setAttributes(List attributes)
Sets the attributes that this element contains

將list中的所有屬性設置到該元素上

 

Attribute接口(實現了Node接口)

QName

getQName()
Returns the QName of this attribute which represents the local name, the qualified name and the Namespace.

獲取屬性名稱

String

getValue()
Returns the value of the attribute.

獲取屬性的值

void

setValue(String value)
Sets the value of this attribute or this method will throw an UnsupportedOperationException if it is read-only.

設置屬性的值

 

 

DocumentHelper 

staticAttribute

createAttribute(Element owner, QName qname, String value)

創建一個Attribute


staticDocument

createDocument()

創建一個Document


staticDocument

createDocument(Element rootElement)

以給定元素作爲根元素創建Document



static Element

createElement(QName qname)

以給定名稱創建一個Element

staticDocument

parseText(String text)
parseTextparses the given text as an XML document and returns the newly created Document.

將一段字符串轉化爲Document


 

 

將節點寫出到XML文件中去

方法1:

調用Node提供的write(Writer writer) 方法,使用默認方式將節點輸出到流中:

         node.write(new FileWriter("book.xml"));

亂碼問題:

Dom4j在將文檔載入內存時使用的是文檔聲明中encoding屬性聲明的編碼集進行編碼,  如果在此時使用writer輸出時writer使用的內部編碼集與encoding不同則會有亂碼問題。

FileWriter默認使用操作系統本地碼錶即gb2312編碼,並且無法更改。

此時可以使用OutputStreamWriter(FileOutputStream("filePath"),"utf-8");的方式自己封裝    一個指定碼錶的Writer使用,從而解決亂碼問題。

方式2:

利用XMLWriter寫出Node:
             XMLWriter writer = new XMLWriter(new  FileWriter("output.xml"));
             writer.write(node);
             writer.close();

亂碼問題:

(1)使用這種方式輸出時,XMLWriter首先會將內存中的docuemnt翻譯成UTF-8          格式的document,在進行輸出,這時有可能出現亂碼問題。

可以使用OutputFormat 指定XMLWriter轉換的編碼爲其他編碼。

              OutputFormat format = OutputFormat.createPrettyPrint();            

            format.setEncoding("GBK");       
              XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),format);

(2)Writer使用的編碼集與文檔載入內存時使用的編碼集不同導致亂碼,使用字節流                 或自己封裝指定編碼的字符流即可(參照方法1)。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章