Java reads XML file Content--turns from looking

Source: Internet
Author: User

XML file

<?xml version= "1.0" encoding= "UTF-8"?><object><student><stuname> Zhang San </stuName>< Stuaddress> Zhengzhou </stuaddress><stutel>111</stutel></student><user><username >aaa</userName><userAge>100</userAge><userSex> Men </userSex></user>< Book><bookname> Chinese </bookname><bookauthor>xxx</bookauthor><bookprice>100</ Bookprice></book></object>

Operation method

Package Com.xml;import Java.io.file;import Java.net.url;import java.util.list;import org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.element;import Org.dom4j.io.saxreader;import Com.bean.Book;import        Com.bean.student;import Com.bean.user;public class Saxxml {public static void main (string[] args) throws Exception {        File File=new file ("Config/student.xml");        Gets the DOM tree URL url=saxdbxml.class.getclassloader (). GetResource ("Student.xml");        Saxreader sax=new Saxreader ();        Document dom=sax.read (file);        Document dom = sax.read (URL);        Gets the root node inside the DOM tree Element rootele=dom.getrootelement ();        Gets the node element under the root node list<element> elelist=rootele.elements ();                        System.out.println (Elelist.size ()); A//loop gets each node element for (int i=0;i<elelist.size (); i++) {            Element Childele=elelist.get (i);            String Name=childele.getname (); if (Name.equalsignorecase ("student")) {               Student stu=new Student ();                Gets the sub-node under the node list<element> childlist=childele.elements ();                    for (int j=0;j<childlist.size (); j + +) {Element ele=childlist.get (j);                    String Elename=ele.getname ();                        if (Elename.equalsignorecase ("Stuname")) {Stu.setstuname (Ele.gettext ());                    System.out.println (Ele.gettext ());                    }else if (elename.equalsignorecase ("stuaddress")) {stu.setstuaddress (Ele.gettext ());                    }else if (elename.equalsignorecase ("Stutel")) {Stu.setstutel (Ele.gettext ()); }}}else if (Name.equalsignorecase ("user")) {User user=new                User ();                Gets the child node element list<element> userlist=childele.elements ();   for (int k=0;k<userlist.size (); k++) {                 Element Userele=userlist.get (k); Determine the name of the child node element, and make the corresponding stored value if (Userele.getname (). Equalsignorecase ("UserName")) {user.se                    Tusername (Userele.gettext ()); }else if (Userele.getname (). Equalsignorecase ("Usersex")) {User.setusersex (US                    Erele.gettext ());                    }else if (Userele.getname (). Equalsignorecase ("Userage")) {User.setuserage (Userele.gettext ()); }}}else if (Name.equalsignorecase ("book")) {Book B                Ook=new book ();                List<element> booklist=childele.elements ();                    for (int k=0;k<booklist.size (); k++) {Element bookele=booklist.get (k);                    String Elename=bookele.getname (); if (Elename.equalsignorecase ("BookName")) {Book.setbookname (Bookele.gettext ());                    }else if (elename.equalsignorecase ("Bookauthor")) {Book.setbookauthor (Bookele.gettext ());                                            }else if (elename.equalsignorecase ("Bookprice")) {Book.setbookprice (Bookele.gettext ());                        }                }                                            } }        }    }

Java reads XML file Content--turns from looking

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.