Java learning: Using dom4j to read and write XML files

Source: Internet
Author: User

Dom4j is an excellent open-source project dedicated to reading and writing XML files and supports powerful XPath.

The followingCodeDemonstrate reading and writing the demo. xml file.

Demo. XML content:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Address-List> <card name = "yangjm" id = "1"> <sex> male </sex> <address> <! [CDATA [Shanghai Bund No. 01]> </address> <telephone> 13916732212 </telephone> </card> <card name = "zhangsan" id = "2"> <sex> female </sex> <address> <item type = "Home Address"> <! [CDATA [China. Beijing. Dongzhimen Street]> </item> <item type = ""> <! [CDATA [China. shanghai. tianlin Road No. 888]> </item> </address> <telephone> 010-123123 </telephone> </card> </address-List>

 

Java code:

Package jmyang. dom4j;/*** <p> title: dom4j test </P> * <p> Description: Use XPath to read node content and attributes </P> * <p> copyright: copyright (c) 2012 </P> * <p> company: infosky.com.cn </P> * @ Author: Yang Guo * @ version 1.0 */import Java under the yjmyzz@126.com bodhi tree. util. *; import Java. io. *; import Org. dom4j. *; import Org. dom4j. io. *; public class xmltest {public static void test () {string filename = system. getproperty ("user. dir ") +" \ demo. XML "; // demo in the current path. XML try {saxreader reader = new saxreader (); document DOC = reader. read (filename); // load the XML file list using LES = Doc. selectnodes ("// * [@ name]"); // select all nodes with the name attribute (demo. all card nodes in XML) for (iterator iter = les. iterator (); ITER. hasnext ();) {element card = (element) ITER. next (); // system. out. println (node. asxml (); // system. out. println ("-------------------------------------"); // system. out. println (node. attributecount (); List attrlist = card. attributes (); // output all attributes of each card for (iterator ATTR = attrlist. iterator (); ATTR. hasnext ();) {attribute a = (attribute) ATTR. next (); system. out. println (. getname () + "=" +. getvalue ();} system. out. println ("----------------------------------------------------");} element zhangsan = (element) Doc. selectsinglenode ("// card [@ ID = '2']"); // find the card element system with "id attribute" = 2. out. println ("Name of zhangsan:" + zhangsan. attribute ("name "). getvalue (); // output the name attribute node addrfamily = zhangsan of zhangsan. selectsinglenode (". /address/item [2] "); // select the System of the 2nd Item subnodes under the address node under the zhangsan element. out. println ("Zhang San's Unit Address:" + addrfamily. getstringvalue (); // output CDATA content system. out. println ("--------------------------------------------------"); // Add two nodes under zhangsan. addelement ("email "). addattribute ("type", "work "). addtext ("work@some-domain.com"); zhangsan. addelement ("email "). addattribute ("type", "private "). addcdata ("private@some-domain.com"); // sets the CDATA content system. out. println (zhangsan. asxml (); // print the XML content of the zhangsan node (for debugging) system. out. println ("--------------------------------------------------"); // Save the preceding changes to the file demo2.xml filewriter = new filewriter (system. getproperty ("user. dir ") +" \ demo2.xml "); outputformat format = outputformat. createprettyprint (); // set the beautiful indent format for ease of reading // format = outputformat. createcompactformat (); // set the compact format (remove unnecessary spaces) to facilitate the download of xmlwriter writer = new xmlwriter (system. out, format); writer. setwriter (filewriter); writer. write (DOC); writer. close ();} catch (exception e) {e. printstacktrace ();}}}

Result of JBuilder:

Name = yangjm
Id = 1
----------------------------------------------------
Name = zhangsan
Id = 2
----------------------------------------------------
Name of Zhang San: zhangsan
Address of James: No. 888, tianlin Road, Shanghai, China
----------------------------------------------------
<Card name = "zhangsan" id = "2">
<Sex> female </sex>
<Address>
<Item type = "Home Address"> <! [CDATA [China. Beijing. Dongzhimen Street]> </item>
<Item type = "Unit Address"> <! [CDATA [888 tianlin Road, Shanghai, China]> </item>
</Address>
<Telephone> 010-123123 </telephone>
<Email type = "work"> work@some-domain.com </Email> <email type = "private"> <! [CDATA [private@some-domain.com]> </Email> </card>
----------------------------------------------------

Saved demo2.xml

<? XML version = "1.0" encoding = "UTF-8"?>

<Address-List>
<Card name = "yangjm" id = "1">
<Sex> male </sex>
<Address> <! [CDATA [China Shanghai Bund No. 01]> </address>
<Telephone> 13916732212 </telephone>
</Card>
<Card name = "zhangsan" id = "2">
<Sex> female </sex>
<Address>
<Item type = "Home Address"> <! [CDATA [China. Beijing. Dongzhimen Street]> </item>
<Item type = "Unit Address"> <! [CDATA [888 tianlin Road, Shanghai, China]> </item>
</Address>
<Telephone> 010-123123 </telephone>
<Email type = "work"> work@some-domain.com </Email>
<Email type = "private"> <! [CDATA [private@some-domain.com]> </Email>
</Card>
</Address-List>

 

 

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.