国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

你所應(yīng)該知道的Dom4J

2019-11-14 22:56:13
字體:
供稿:網(wǎng)友
你所應(yīng)該知道的Dom4J

創(chuàng)建解析器:

SAXReader reader = new SAXReader();

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

獲取文檔的根節(jié)點(diǎn):

Element root =document.getRootElement();

接口繼承結(jié)構(gòu):

Node ---

Branch

--Document

--Element

----

Attribute

Node接口

String

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

將一個節(jié)點(diǎn)轉(zhuǎn)換為字符串

String

getName() getNamereturns the name of this node.

獲取節(jié)點(diǎn)的名稱,如果是元素則獲取到元素名,如果是屬性獲取到屬性名

short

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

獲取節(jié)點(diǎn)類型,在Node接口上定義了一些靜態(tài)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.

獲取父節(jié)點(diǎn),如果是根元素調(diào)用則返回null,如果是其他元素調(diào)用則返回父元素,如果是屬性調(diào)用則返回屬性所依附的元素。

String

getText() Returns the text of this node.

返回節(jié)點(diǎn)文本,如果是元素則返回標(biāo)簽體,如果是屬性則返回屬性值

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表達(dá)式,選擇節(jié)點(diǎn)

void

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

設(shè)置節(jié)點(diǎn)的名稱,元素可以更改名稱,屬性則不可以,會拋出UnsupportedOperationException 異常

void

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

設(shè)置節(jié)點(diǎn)內(nèi)容,如果是元素則設(shè)置標(biāo)簽體,如果是屬性則設(shè)置屬性的值

void

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

將節(jié)點(diǎn)寫出到一個輸出流中,元素、屬性均支持

Branch接口(實現(xiàn)了Node接口)

void

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

增加一個子節(jié)點(diǎn)

Element

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

增加一個給定名字的子節(jié)點(diǎn),并且返回這個新創(chuàng)建的節(jié)點(diǎn)的引用

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.

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

boolean

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

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

Element接口(實現(xiàn)了Branch, Node接口)

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.

獲取元素標(biāo)簽體

boolean

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

移除元素上的屬性

void

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

將list中的所有屬性設(shè)置到該元素上

Attribute接口(實現(xiàn)了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.

設(shè)置屬性的值

DocumentHelper

static Attribute

createAttribute(Element owner, QName qname, String value)

創(chuàng)建一個Attribute

static Document

createDocument()

創(chuàng)建一個Document

static Document

createDocument(Element rootElement)

以給定元素作為根元素創(chuàng)建Document

static Element

createElement(QName qname)

以給定名稱創(chuàng)建一個Element

static Document

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

將一段字符串轉(zhuǎn)化為Document

將節(jié)點(diǎn)寫出到XML文件中去

方法1:

調(diào)用Node提供的write(Writerwriter) 方法,使用默認(rèn)方式將節(jié)點(diǎn)輸出到流中:

node.write(newFileWriter("book.xml"));

亂碼問題:

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

FileWriter默認(rèn)使用操作系統(tǒng)本地碼表即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首先會將內(nèi)存中的docuemnt翻譯成UTF-8 格式的document,在進(jìn)行輸出,這時有可能出現(xiàn)亂碼問題。

可以使用OutputFormat 指定XMLWriter轉(zhuǎn)換的編碼為其他編碼。

OutputFormat format =OutputFormat.createPrettyPrint();

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

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿坝| 永康市| 南澳县| 泰宁县| 岗巴县| 玛纳斯县| 余姚市| 南川市| 山丹县| 厦门市| 当雄县| 安顺市| 伊宁县| 浪卡子县| 香格里拉县| 新竹县| 珠海市| 绩溪县| 库车县| 宜丰县| 伊宁县| 扎兰屯市| 梅河口市| 广丰县| 治县。| 阿图什市| 蓬莱市| 惠水县| 正阳县| 西畴县| 彩票| 忻州市| 临江市| 象山县| 民勤县| 通城县| 响水县| 洪湖市| 临西县| 铜山县| 上栗县|