DOM4J Parsing xml

Source: Internet
Author: User

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:

 
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <users>
  3. <user username= "Lisi" password="123"/>
  4. <user username= "Zhang San" password="123"/>
  5. <user username= "zhangsan" password="123"/>
  6. </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:

 
  1. Package Com.easyteam.dao;
  2. Import java.io.FileNotFoundException;
  3. Import Java.io.FileOutputStream;
  4. Import java.io.IOException;
  5. Import Java.io.OutputStream;
  6. Import Java.io.OutputStreamWriter;
  7. Import java.io.UnsupportedEncodingException;
  8. Import org.dom4j.Document;
  9. Import org.dom4j.DocumentException;
  10. Import org.dom4j.Element;
  11. Import Org.dom4j.io.OutputFormat;
  12. Import Org.dom4j.io.SAXReader;
  13. Import Org.dom4j.io.XMLWriter;
  14. Import Com.easyteam.domain.User;
  15. Public class Userdao {
  16. String path="D://user.xml";
  17. Public User Findbyname (String username) {
  18. Saxreader red=New Saxreader (); Creating a parser
  19. try {
  20. Document doc=red.read (path); //Get Documet object
  21. Element el= (Element) Doc.selectsinglenode ("//user[@username = '" +username+"']");//Query condition, where//denotes infinite depth query, [] Put in is a query condition
  22. if (el==null) return null;
  23. String attusername=el.attributevalue ("username"); Get the value of username this property
  24. String attpassword=el.attributevalue ("password");
  25. User user=New user ();
  26. User.setusername (Attusername);
  27. User.setpassword (Attpassword);
  28. return user;
  29. } catch (Documentexception e) {
  30. throw New RuntimeException (e);
  31. }
  32. }
  33. public void AddUser (user user) {
  34. Saxreader red=New Saxreader ();
  35. try {
  36. Document doc = red.read (path);
  37. Element attroot=doc.getrootelement (); //Get root node
  38. Element userel=attroot.addelement ("user"); Adding nodes
  39. Userel.addattribute ("username", user.getusername ()); Assigning a value to a new node
  40. Userel.addattribute ("Password", User.getpassword ());
  41. OutputFormat format=New OutputFormat ("\ T", true); Set format
  42. Format.settrimtext (true); Clear original format
  43. XMLWriter writer;
  44. try {
  45. writer=New XMLWriter (New OutputStreamWriter (NewFileOutputStream (path),"Utf-8"), format); Instantiation of
  46. Writer.write (DOC); //Save
  47. Writer.close ();
  48. } catch (Exception e) {
  49. throw New RuntimeException (e);
  50. }
  51. } catch (Documentexception e) {
  52. throw New RuntimeException (e);
  53. }
  54. }
  55. }

3. Test class:

 
  1. Package com.easyteam.test;
  2. Import Org.junit.Test;
  3. Import Com.easyteam.dao.Userdao;
  4. Import Com.easyteam.domain.User;
  5. Public class Userdaotest {
  6. @Test
  7. Public void Testfindbyname () {
  8. Userdao dao=New Userdao ();
  9. User user= dao.findbyname ("Lisi");
  10. SYSTEM.OUT.PRINTLN (user);
  11. }
  12. @Test
  13. Public void Testadduser () {
  14. Userdao dao=New Userdao ();
  15. User user=New user ();
  16. User.setusername ("Zhang San");
  17. User.setpassword ("123");
  18. Dao.adduser (user);
  19. }
  20. }

DOM4J Parsing xml

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.