Parsing an XML file with dom4j and reading an XML file

Source: Internet
Author: User

XML file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Bookstore>3     < BookID= "1">4         <name>The song of Ice and Fire</name>5         <author>George Martin</author>6         < Year>2014</ Year>7         < Price>89</ Price>8     </ Book>9     < BookID= "2">Ten         <name>Andersen Fairy Tale</name> One         < Year>2004</ Year> A         < Price>77</ Price> -         <language>中文版</language> -     </ Book>     the </Bookstore>

Book entity class

1  Public classBook {2     PrivateString ID;3     PrivateString name;4     PrivateString author;5     PrivateString year;6     PrivateString Price;7     PrivateString language;8     9     //Get Set method generation can beTen      One}

DOM4J parsing an XML file

1  Public classDom4jtest {2     Private StaticArraylist<book> Booklist =NewArraylist<book>();3 4     /**5      * @paramargs6      */7      Public Static voidMain (string[] args) {8         //parsing books.xml Files9         //Create a Saxreader object ReaderTenSaxreader reader =NewSaxreader (); One         Try { A             //gets the Docuemnt object by loading the Books.xml file with the Read method of the reader object.  -Document document = Reader.read (NewFile ("Src/res/books.xml")); -             //gets the root node through the Document Object Bookstore theElement Bookstore =document.getrootelement (); -             //getting iterators through the Elementiterator method of the Element object -Iterator it =bookstore.elementiterator (); -             //iterate through iterators to get information from the root node (books) +              while(It.hasnext ()) { -System.out.println ("===== begins to traverse a book ====="); +Element book =(Element) It.next (); A                 //get the property name and property value of book atList<attribute> Bookattrs =book.attributes (); -                  for(Attribute attr:bookattrs) { -System.out.println ("Property name:" + attr.getname () + "--Property value:" +Attr.getvalue ()); -                 } -Iterator ITT =book.elementiterator (); -                  while(Itt.hasnext ()) { inElement Bookchild =(Element) Itt.next (); -System.out.println ("Node name:" + bookchild.getname () + "--node value:" +Bookchild.getstringvalue ()); to                 } +System.out.println ("===== ends the traversal of a book ====="); -             } the}Catch(documentexception e) { *             //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng         } -     } the}

DOM4J reading XML

 Public classDom4jtest {Private voidCreatexml () {//1. Create a Document object that represents the entire XML documentDocument document =documenthelper.createdocument (); //2. Create a root node rssElement RSS = document.addelement ("RSS"); //3. Add the Version property to the RSS nodeRss.addattribute ("Version", "2.0"); //4. Generating child nodes and node contentElement channel = rss.addelement ("channel"); Element title= Channel.addelement ("title"); Element content= Title.addelement ("Content"); Content.settext ("The northern part of the week is noticeably warmer and more than 8 ℃ higher than the same period."); //Title.settext ("<![        cdata[Shanghai Mobile Internet Industry Promotion Center officially unveiled]]> "); //5. Set the format of the generated XMLOutputFormat format =Outputformat.createprettyprint (); Format.setencoding ("UTF-8"); //6. Generating an XML fileFile File =NewFile ("Rssnews.xml");        XMLWriter writer; Try{writer=NewXMLWriter (Newfileoutputstream (file), format); //sets whether to escape, the default value is True, which represents the escapeWriter.setescapetext (false);            Writer.write (document);        Writer.close (); } Catch(IOException e) {e.printstacktrace (); }    }    /**     * @paramargs*/     Public Static voidMain (string[] args) {Newdom4jtest (). Createxml (); }}
Read XML

Parsing an XML file with dom4j and reading an XML file

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.