parsing XML files with dom4j in Java

Source: Internet
Author: User

Official website Download dom4j Address: https://dom4j.github.io/
Note: To use dom4j development, download dom4j corresponding jar file

title: The backend uses dom4j to parse the Student.xml file and return to the List<student> collection

XML to parse:

<?XML version= "1.0" encoding= "UTF-8"?><Classes>    <classname= "ST01">        <StudentID= "10001">User1</Student>        <StudentID= "10002">User2</Student>        <StudentID= "10003">User3</Student>    </class>    <classname= "ST02">        <StudentID= "10004">User4</Student>        <StudentID= "10005">User5</Student>        <StudentID= "10006">User6</Student>    </class>    <classname= "ST03">        <StudentID= "10007">User7</Student>        <StudentID= "10008">User8</Student>        <StudentID= "10009">User9</Student>    </class>    <classname= "ST04">        <StudentID= "10010">User10</Student>        <StudentID= "10011">User12</Student>        <StudentID= "10012">User13</Student>    </class>    <classname= "ST05">        <StudentID= "10013">User14</Student>        <StudentID= "10014">User15</Student>        <StudentID= "10015">User16</Student>    </class></Classes>

Java Parsing XML code:

/** * @authorhh*/ Public classXmlutil {/*** Read XML student list information *@return     */     PublicList<student>liststudent () {//Create a Saxreader objectSaxreader reader=NewSaxreader (); Document Document=NULL; Try {            //read a file into a Document object by using the Read methodDocument = Reader.read (NewFile ("/users/mac/documents/students.xml")); } Catch(documentexception e) {e.printstacktrace (); }        //gets the root node element objectElement node =document.getrootelement (); returnElementmethod (node); }    /*** Get information from a node *@paramnode *@return     */    PrivateList<student>Elementmethod (Element node) {List<Student> list =NewArraylist<student>(); //get all class nodeslist<element> Elementclass = node.elements ("Class");  for(Element aclass:elementclass) {//get all student nodesList<element> elelmentstu=aclass.elements ("Student");  for(Element element:elelmentstu) {//Create student ObjectsStudent stu =NewStudent (); //the value of name in the class nodeStu.setsclazz (Aclass.attribute ("name"). GetValue ()); //the value of the ID in the student nodeStu.setsid (Element.attribute ("id"). GetValue ()); //string value in the student nodeStu.setsname (Element.getstringvalue ());            List.add (Stu); }        }        returnlist; }}

Entity class:

/** * @authorhh*/ Public classStudent {PrivateString sname; PrivateString SID; PrivateString Sclazz;  PublicString Getsname () {returnsname; }     Public voidsetsname (String sname) { This. sname =sname; }     PublicString GetSID () {returnSID; }     Public voidSetsid (String sid) { This. Sid =SID; }     PublicString Getsclazz () {returnSclazz; }     Public voidSetsclazz (String sclazz) { This. Sclazz =Sclazz; } @Override PublicString toString () {return"student{" + "sname=" + sname + "\" + ", sid= '" + sid + "\" + ", sclazz= '" + sclazz + ' + ' + '} '; }}
View Code

@Test Code:

 Public class studentdaotest {    @Test    publicvoid  testliststudent () {        ListNew  Xmlutil (). Liststudent ();          for (Student student:list) {            System.out.println (Student);     }}}
View Code

Operation Result:

parsing XML files with dom4j in Java

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.