What You Should Know About Dom4J and Dom4J

Source: Internet
Author: User

What You Should Know About Dom4J and Dom4J

Create a parser:

SAXReader reader = new SAXReader ();

Use the parser to read xml documents:
Document document = reader. read (new File ("input. xml "));

Get the root node of the document:

Element root = document. getRootElement ();

Interface inheritance structure:

Node ---

Branch

-- Document

-- Element

----

Attribute

 

NodeInterface

String

AsXML()
AsXMLreturns the textual XML representation of this node.

Converts a node to a string.

String

GetName()
GetNamereturns the name of this node.

Obtains the node name. If it is an element, the element name is obtained. If it is an attribute, the attribute name is obtained.

Short

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

Obtains the Node type. Some static short constants are defined on the Node interface to indicate various types.

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.

Obtain the parent node. If it is called by the root element, null is returned. If it is called by other elements, the parent element is returned. If it is called by the attribute, the elements attached to the attribute are returned.

String

GetText()
Returns the text of this node.

Return the node text. If it is an element, the TAG body is returned. If it is an attribute, the attribute value is returned.

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.

Select a node using an xpath expression

Void

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

Set the node name. The element can change the name, but the attribute cannot. An UnsupportedOperationException is thrown.

Void

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

Set the node content. If it is an element, set the label body. If it is an attribute, set the attribute value.

Void

Write(Writer writer)
Writewrites this node as the default XML notation for this node.

Writes a node to an output stream. All elements and attributes are supported.

 

 

BranchInterface(ImplementedNodeInterface)

Void

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

Add a subnode

Element

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

Add a subnode with a given name and return a reference to the newly created 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.

Obtains the position number of a given node in all direct points. If the node is not a subnode of this branch,-1 is returned.

Boolean

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

Return a Boolean value indicating whether the deletion is successful.

 

ElementInterface (implementedBranch,NodeInterface)

Void

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

Add an attribute

Element

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

Adds an attribute to an element, uses the given attribute name and attribute value, and returns the element

Element

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

Add attribute to element

Attribute

Attribute(Int index)
Returns the attribute at the specified indexGets

Obtains the attribute of a specified position.

Attribute

Attribute(QName qName)
Document me!

Obtains the attributes of a specified name.

Iterator

AttributeIterator()
Document me!

Get attribute iterator

List

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

Obtains all the attributes of the element and returns a 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.

Obtains the value of the specified name attribute. If this attribute does not exist, null is returned. If this attribute exists but the attribute value is null, an empty string is returned.

Element

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

Obtains the child element of the specified name. If there are multiple child elements of the name, the first child element is returned.

Element

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

Obtains the child element of the specified name. If there are multiple child elements of the name, the first child element is returned.

Iterator

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

Get child element iterator

Iterator

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

Iterator for retrieving child elements of a specified name

List

Elements()
Returns the elements contained in this element.

Obtain all child elements and return them with a list

List

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

Obtain all the child elements of the specified name and return them with a list.

String

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

Get element TAG body

Boolean

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

Remove attributes from an element

Void

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

Set all attributes in the list to this element.

 

AttributeInterface(ImplementedNodeInterface)

QName

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

Get attribute name

String

GetValue()
Returns the value of the attribute.

Get Attribute Value

Void

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

Set attribute values

 

 

DocumentHelperClass

Static Attribute

CreateAttribute(Element owner, QName qname, String value)

Create an Attribute

 

Static Document

CreateDocument()

Create a Document

 

Static Document

CreateDocument(Element rootElement)

Create a Document with a given element as the root element

 

 

Static Element

CreateElement(QName qname)

Create an Element with a given name

Static Document

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

Convert a string to a Document

 

 

 

Write the nodeXMLFile

Method 1:

Call the write (Writerwriter) method provided by Node to output the Node to the stream by default:

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

Garbled problem:

Dom4j uses the encoding set of the encoding attribute declaration in the Document declaration when loading documents into memory for encoding, if the internal collation set used by writer is different from encoding when writer is output at this time, garbled characters may occur.

FileWriter uses the local code table (gb2312) of the operating system by default and cannot be changed.

In this case, you can use OutputStreamWriter (FileOutputStream ("filePath"), "UTF-8"); to encapsulate the Writer of a specified code table, so as to solve the garbled problem.

Method 2:

Use XMLWriter to write the Node:
XMLWriter writer = new XMLWriter (new FileWriter ("output. xml "));
Writer. write (node );
Writer. close ();

Garbled problem:

(1) When output in this way, XMLWriter first translates docuemnt in memory into a document in the UTF-8 format and outputs it in the input line. At this time, garbled characters may occur.

You can use OutputFormat to specify other encodings for XMLWriter conversion.

OutputFormat format = OutputFormat. createPrettyPrint ();

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

(2) The encoding set used by Writer is different from the encoding set used when the document is loaded into memory, leading to garbled characters. You can use a byte stream or encapsulate the encoding stream specified by yourself (refer to method 1 ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.