"Note--java" dom4j Operation notes

Source: Internet
Author: User
Tags gettext

A well-known extension of dom4j used to manipulate XML

Website: http://www.dom4j.org/dom4j-1.6.1/

After downloading, import the Dom4j-1.6.1.jar into the project extension

One: Read

dom4j using saxreader for reading

1  New Saxreader ();

The object Reader has a method reader () method

Choose a different refactoring method based on the location of the XML file.

A org.dom4j.Document object is returned after reading

1 org.dom4j.Document cfg = reader.read (filecfg);

1. Get the node:

The CFG object is preceded by Getrootelement () to get the root node of the XML.

Each node is then obtained through the element (String elementname) method, and the parameter name is the name of the node

For example:

1 Element win = root.element ("window");

You can get the node "window", at which time the win represents the node

2. Get the attribute values for a node:

If you need to get the properties of the node, use AttributeValue (String attributename) to get

For example:

1 String attributevalue = Win.attributevalue ("width");

You can get the property value.

3. Get the text of a node

If you need to get the text, get it through the GetText () method of the Node object

1 String text = Win.gettext ();

The above is read content.

Second, create the XML file:

Like reading, you create a org.dom4j.Document object, write it, and then write to the document.

1. Create a org.dom4j.Document object

1 // Get Factory 2 documentfactory newcfg = documentfactory.getinstance (); 3 // Create Document Object 4 Document doc = Newcfg.createdocument ();

The next step is simple.

1. Add a node:

1 Element window = doc.addelement ("window");

2. Adding attributes

1 window.addattribute ("Size", "1270");

3. Add text addText (string text) or addcdata (string text)

/* * This is to add plain text, that is, add results to: * <window> This is the window </window> */ Window.addtext ("This is the Window"); /* * This will add something to the text, that is, add the result: * <window>! [cdata[This is the window]]</window>*/Window.addcdata ("This is the Window");

Note: adding attributes, or adding text, is for a node.

4. Write the XML file and save the created XML to the file.

Suppose there is an XML file path of Data/test.xml;

First create the Output object:

1 New XMLWriter ( new FileOutputStream ( "Data/test.xml"));

This class reconstructs a number of constructors for easy writing to various places

The object's write (document DOC) method is then invoked to write the created XML object to the file.

6. Format the output text for easy viewing

// Set file Encoding   New OutputFormat ();  Xmlformat.setencoding ("UTF-8"//xmlformat.setnewlines (true  //  xmlformat.setindent (true//xmlformat.setindent ("    

When you create a XMLWriter object, you can put it in the constructor as a parameter.

New XMLWriter ( new FileOutputStream ( "Data/test.xml"), Xmlformat);

Third, modify the XML:

Modification is more simple, first read the XML file as a document object, and then the modification of the place to modify, and then write back to the file can be, here is not a statement.

"Note--java" dom4j Operation notes

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.