Now there are a lot of toolkit to parse XML files, but dom4j is undoubtedly one of the simpler, here is an example to actually operate:
First you have to import Dom4j.jar this clip (online search a large)
1. Create a user.xml file in one place, which I am building in the D Packing directory:
- <? XML version= "1.0" encoding="UTF-8"?>
- <users>
- <user username= "Lisi" password="123"/>
- <user username= "Zhang San" password="123"/>
- <user username= "zhangsan" password="123"/>
- </users>
<?xml version= "1.0" encoding= "UTF-8"? ><users><user username= "Lisi" password= "123"/><user Username= "Zhang San" password= "123"/><user username= "Zhangsan" password= "123"/></users>
2. Impersonate the file to query the user (query by name) and add Users:
- Package Com.easyteam.dao;
- Import java.io.FileNotFoundException;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.io.OutputStream;
- Import Java.io.OutputStreamWriter;
- Import java.io.UnsupportedEncodingException;
- Import org.dom4j.Document;
- Import org.dom4j.DocumentException;
- Import org.dom4j.Element;
- Import Org.dom4j.io.OutputFormat;
- Import Org.dom4j.io.SAXReader;
- Import Org.dom4j.io.XMLWriter;
- Import Com.easyteam.domain.User;
- Public class Userdao {
- String path="D://user.xml";
- Public User Findbyname (String username) {
- Saxreader red=New Saxreader (); Creating a parser
- try {
- Document doc=red.read (path); //Get Documet object
- Element el= (Element) Doc.selectsinglenode ("//user[@username = '" +username+"']");//Query condition, where//denotes infinite depth query, [] Put in is a query condition
- if (el==null) return null;
- String attusername=el.attributevalue ("username"); Get the value of username this property
- String attpassword=el.attributevalue ("password");
- User user=New user ();
- User.setusername (Attusername);
- User.setpassword (Attpassword);
- return user;
- } catch (Documentexception e) {
- throw New RuntimeException (e);
- }
- }
- public void AddUser (user user) {
- Saxreader red=New Saxreader ();
- try {
- Document doc = red.read (path);
- Element attroot=doc.getrootelement (); //Get root node
- Element userel=attroot.addelement ("user"); Adding nodes
- Userel.addattribute ("username", user.getusername ()); Assigning a value to a new node
- Userel.addattribute ("Password", User.getpassword ());
- OutputFormat format=New OutputFormat ("\ T", true); Set format
- Format.settrimtext (true); Clear original format
- XMLWriter writer;
- try {
- writer=New XMLWriter (New OutputStreamWriter (NewFileOutputStream (path),"Utf-8"), format); Instantiation of
- Writer.write (DOC); //Save
- Writer.close ();
- } catch (Exception e) {
- throw New RuntimeException (e);
- }
- } catch (Documentexception e) {
- throw New RuntimeException (e);
- }
- }
- }
3. Test class:
- Package com.easyteam.test;
- Import Org.junit.Test;
- Import Com.easyteam.dao.Userdao;
- Import Com.easyteam.domain.User;
- Public class Userdaotest {
- @Test
- Public void Testfindbyname () {
- Userdao dao=New Userdao ();
- User user= dao.findbyname ("Lisi");
- SYSTEM.OUT.PRINTLN (user);
- }
- @Test
- Public void Testadduser () {
- Userdao dao=New Userdao ();
- User user=New user ();
- User.setusername ("Zhang San");
- User.setpassword ("123");
- Dao.adduser (user);
- }
- }
DOM4J Parsing xml