Java uses DOM4J to manipulate XML sample code _java

Source: Internet
Author: User
Tags gettext readline

DOM4J is a very good Java XML API with excellent performance, powerful features and extreme ease of use, and it is also an open source tool. Can be downloaded at this address http://dom4j.sourceforge.net.
The dom4j we use here is dom4j-1.6.1 this version, we only need to use the following two jar packages:

Copy Code code as follows:

Dom4j-1.6.1.jar
Commons-io-2.4.jar

1. DOM4J Read XML strings

Copy Code code as follows:

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;


public class Testreadxmlstring {
public static void Main (string[] args) throws Documentexception {
String readline = "<?xml version=\" 1.0\ encoding=\ "utf-8\" ><students><student sid=\ "001\" > <id >001</id><name> Grey Machine </name> <age>18</age> </student></students> ";
Document document = Documenthelper.parsetext (ReadLine);
Element Rootelm = Document.getrootelement ();
System.out.println ("rootelement:" + rootelm.getname ());
Element student = rootelm.element ("student");
Element id = student.element ("id");
Element name = student.element ("name");
Element age = student.element (' age ');
System.out.println (Id.gettext ());
System.out.println (Name.gettext ());
System.out.println (Age.gettext ());
}
}

2. dom4j Create XML file

Copy Code code as follows:

Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.OutputFormat;
public class Testwritexmlstring {
public static void Main (string[] args) {
OutputFormat format = Outputformat.createprettyprint ();
1. Construct an empty document
Document doc = Documenthelper.createdocument ();
Doc.addcomment ("This is a comment");
2. Constructing the root element
Element ROOTELMT = doc.addelement ("users");
Rootelmt.addnamespace ("Test", "www.test.com");

Element USERELMT = rootelmt.addelement ("user");
Userelmt.addattribute ("Number", "1001");
Userelmt.addelement ("name"). SetText ("Zhangsan");
Userelmt.addelement ("Age"). SetText ("20");
Userelmt.addelement ("Gender"). SetText ("Mail");

Element userElmt2 = rootelmt.addelement ("user");
Userelmt.addattribute ("number", "1002");
Userelmt2.addelement ("name"). SetText ("Zhangsan");
Userelmt2.addelement ("Age"). SetText ("20");
Userelmt2.addelement ("Gender"). SetText ("Mail");

System.out.println (Doc.asxml (). ReplaceAll ("\ n", ""));
}
}

3. Read or write XML files

reading XML files

Copy Code code as follows:

Saxreader reader = new Saxreader ();
String Path = "E:/workspaces/myeclipse 8.6/xmltest/file/student.xml";
Document document = Reader.read (new File (path));

Write XML file

Copy Code code as follows:

OutputFormat format = Outputformat.createprettyprint ();
Format.setencoding ("Utf-8");//Set the encoding format of the XML file
String FilePath = "E:/workspaces/myeclipse 8.6/xmltest/file/student.xml";
Document document = Documenthelper.createdocument ();
Doc.addcomment ("This is a comment");

/Create Document Content

Copy Code code as follows:

XMLWriter writer = new XMLWriter (new FileWriter (FilePath), format);//write to specified file
Writer.write (document);
Writer.close ();

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.