你所应该知道的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接口(实现了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.

获取元素标签体

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

static Attribute

createAttribute(Element owner, QName qname, String value)

创建一个Attribute

 

static Document

createDocument()

创建一个Document

 

static Document

createDocument(Element rootElement)

以给定元素作为根元素创建Document

 

 

static Element

createElement(QName qname)

以给定名称创建一个Element

static Document

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);

2Writer使用的编码集与文档载入内存时使用的编码集不同导致乱码,使用字节流                   或自己封装指定编码的字符流即可(参照方法1)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值