Dom4j parses XML instances

Source: Internet
Author: User

Dom4j is a Java xml api, similar to JDOM, used to read and write XML files. It features excellent performance, powerful functionality, and ease of use.

Jar package: dom4j-1.6.1.jar

XML file to be parsed: people. xml

 

XML Code
  1. <People city = "Shenzhen">
  2. <Student name = "Milton" age = "22"> </student>
  3. <Student name = "lego" age = "23"> </student>
  4. <Teacher name = "Bruce" age = "27"> </teacher>
  5. <Teacher name = "Ziven" age = "28"> </teacher>
  6. </People>

First, build a JavaBean according to the XML document.

People. Java

Student. Java

Teacher. Java

Parsing Code:

 

Java code
  1. Package com. Test. xml;
  2. Import java. Io. file;
  3. Import java.net. urisyntaxexception;
  4. Import java. util. arraylist;
  5. Import java. util. iterator;
  6. Import java. util. List;
  7. Import org. dom4j. Document;
  8. Import org. dom4j. extends entexception;
  9. Import org. dom4j. element;
  10. Import org. dom4j. Io. saxreader;
  11. Import com. Test. xml. element. People;
  12. Import com. Test. xml. element. student;
  13. Import com. Test. xml. element. teacher;
  14. Public class xmlparser {
  15. @ Suppresswarnings ("unchecked ")
  16. Public static people parsexml (string xmlpath ){
  17. File xmlfile = new file (xmlpath );
  18. System. Out. println (xmlfile. getpath ());
  19. If (xmlfile. exists ()){
  20. Saxreader reader = new saxreader ();
  21. People = new people ();
  22. Try {
  23. // Read the document stream
  24. Document document = reader. Read (xmlfile );
  25. // Obtain the root node
  26. Element root = Document. getrootelement ();
  27. List <student> students = new arraylist <student> ();
  28. List <teacher> teachers = new arraylist <teacher> ();
  29. People. setcity (root. attributevalue ("city "));
  30. // Parse the student Node
  31. For (iterator = root. elementiterator ("student"); iterator. hasnext ();){
  32. Element estudent = (element) iterator. Next ();
  33. Student = new student (estudent. attributevalue ("name"), integer. parseint (estudent. attributevalue ("Age ")));
  34. Students. Add (student );
  35. }
  36. // Parse the teacher Node
  37. For (iterator = root. elementiterator ("teacher"); iterator. hasnext ();){
  38. Element eteacher = (element) iterator. Next ();
  39. Teacher = new teacher (eteacher. attributevalue ("name"), integer. parseint (eteacher. attributevalue ("Age ")));
  40. Teachers. Add (teacher );
  41. }
  42. People. setstudent (students );
  43. People. setteacher (teachers );
  44. System. Out. println ("parse success! ");
  45. } Catch (incluentexception e ){
  46. E. printstacktrace ();
  47. }
  48. Return people;
  49. } Else {
  50. System. Out. println ("file is not exist! ");
  51. Return NULL;
  52. }
  53. }
  54. Public static void main (string [] ARGs) throws urisyntaxexception {
  55. String xmlpath = "";
  56. Xmlpath = "D:/program files/eclipse-jee/workspace/test/src/COM/test/XML/people. xml ";
  57. System. Out. println ("file full path is" + xmlpath );
  58. // Parse people. xml
  59. People P = parsexml (xmlpath );
  60. // Process resolution results
  61. System. Out. println (P. getcity ());
  62. List <student> slist = P. getstudent ();
  63. List <teacher> tlist = P. getteacher ();
  64. For (student s: slist ){
  65. System. Out. println (S. getname () + "--" + S. getage ());
  66. }
  67. For (teacher T: tlist ){
  68. System. Out. println ();
  69. }
  70. }
  71. }
Java code
  1. Running result:
Java code
  1. Parse success!
  2. Shenzhen
  3. Milton -- 22
  4. Lego -- 23

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.