Dom4j getting started

Source: Internet
Author: User

Today, the project needs to process the captured XML file into a Java class, so it is expected to use dom4j.

The following example serves as an entry guide.

Our XML file:

<?xml version="1.0" encoding="UTF-8"?>  <books>      <!--This is a test for dom4j, holen, 2004.9.11-->      <book show="yes">          <title>Dom4j Tutorials</title>      </book>      <book show="yes">          <title>Lucene Studing</title>      </book>      <book show="no">          <title>Lucene in Action</title>      </book>      <owner>O'Reilly</owner>  </books> 

Use dom4j to create an XML document:

Public static int createxmlfile (string filename) {// If the returned value is 1, int returnvalue = 0; // you can create a Document Object document = enenthelper. createdocument (); // create the books node element bookselement = document. addelement ("books"); // Add a comment bookselement. addcomment ("this is a test for dom4j"); // Add the book node element bookelement = bookselement. addelement ("book"); // Add the attribute bookelement to the book node. addattribute ("show", "yse"); // Add the subnode titleelement = bookelement to the book node. addelement ("title"); // sets the value of the title node titleelement. settext ("dom4j tutorials"); bookelement = bookselement. addelement ("book"); bookelement. addattribute ("show", "yes"); titleelement = bookelement. addelement ("title"); titleelement. settext ("Lucene studing"); bookelement = bookselement. addelement ("book"); bookelement. addattribute ("show", "no"); titleelement = bookelement. addelement ("title"); titleelement. settext ("Lucene in action"); element ower = bookselement. addelement ("owner"); ower. settext ("Reilly"); try {xmlwriter writer = new xmlwriter (New filewriter (new file (filename); writer. write (document); writer. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} return returnvalue ;}

Modify the XML document:

Public static int modixmlfile (string filename, string newfilename) {int returnvalue = 0; saxreader = new saxreader (); try {// read the filename file and obtain a documentdocument document = saxreader. read (new file (filename); List list = document. selectnodes ("/books/book/@ show"); iterator iter = List. iterator (); While (ITER. hasnext () {attribute = (attribute) ITER. next (); If (attribute. getvalue (). equals ("Yes") {// change the property of the show to Yes value to noattribute. setvalue ("no") ;}// change the value of the owner node to tishinghua. Add the child node dateelement for the ower node. Set the value of the dateelement node to, and add the attribute typelist = document. selectnodes ("Books/owner"); iter = List. iterator (); If (ITER. hasnext () {element owerelement = (element) ITER. next (); owerelement. settext ("tshinghua"); element dateelement = owerelement. addelement ("date"); dateelement. settext ("2011-11-11"); dateelement. addattribute ("type", "freforian Calendar");} // Delete list = Document from the title node with the value of dom4j tutorials. selectnodes ("Books/book"); iter = List. iterator (); While (ITER. hasnext () {element bookelement = (element) ITER. next (); iterator = bookelement. elementiterator ("title"); While (iterator. hasnext () {element titleelement = (element) iterator. next (); If (titleelement. gettext (). equals ("dom4j tutorials") {bookelement. remove (titleelement) ;}} xmlwriter wR = new xmlwriter (New filewriter (new file (newfilename); Wr. write (document); Wr. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (effecentexception e) {// todo auto-generated catch blocke. printstacktrace ();} return returnvalue ;}

Format the XML document for easy viewing:

public static int formatXMLFile(String filename) {int returnValue = 0;XMLWriter writer = null;SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File(filename));OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("GBK");writer = new XMLWriter(new FileWriter(new File(filename)), format);writer.write(document);writer.close();returnValue = 1;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}return returnValue;}

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.