Analysis of how to parse XML document into Java object (to be sorted)

Source: Internet
Author: User

User class

 Public classUserImplementsSerializable {Private Static Final LongSerialversionuid = 7506854293380407436L; PrivateInteger ID; PrivateString LoginName; PrivateString password; PrivateDate CreateDate; PrivateString username;  PublicUser () {} PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString Getloginname () {returnLoginName; }     Public voidsetloginname (String loginName) { This. LoginName =LoginName; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicDate getcreatedate () {returnCreateDate; }     Public voidsetcreatedate (Date createdate) { This. CreateDate =CreateDate; }     PublicString GetUserName () {System.out.println ("GetUser method in the user class"); returnusername; }     Public voidSetusername (String username) { This. Username =username; } @Override PublicString toString () {return"User [id=" + ID + ", loginname=" + LoginName + ", password=" + password + ", createdate=" +CreateDate+ ", username=" + username + "]"; }

XML Document: Admin.xml

<?XML version= "1.0" encoding= "UTF-8"?><UserID= "1">    <LoginName>[Email protected]</LoginName>    <Password>123123</Password>    <username>Super Admin</username>    <CreateDate>2017-03-07</CreateDate></User>

Parsing an XML document into a Java object

 Public classXmlparser { Public Static voidMain (string[] args)throwsException {//To populate the user objectUser User =NewUser (); //gets the user's class object, followed by the use of reflectionClass Clazz =User.getclass (); File File=NewFile ("D:\\jsd1707\\xtt\\workspace\\rams\\src\\main\\java\\admin.xml"); Saxreader Reader=NewSaxreader (); //read the XML file into documentDocument doc =reader.read (file); //gets the root node of the XML fileElement rootelement =doc.getrootelement (); System.out.println ("Root node:" +rootelement.getname ()); //get all the properties of the root nodeList<attribute> attrlist =rootelement.attributes ();  for(Attribute attribute:attrlist) {//root Node A property nameString Attrname =Attribute.getname (); System.out.println ("Current node Property name:" +attrname); //root Node A property valueString AttrValue =Attribute.getvalue (); //setter method for locating corresponding property in user object by property nameField filed =Clazz.getdeclaredfield (attrname); Class CLA= Filed.gettype ();//Property TypeSystem.out.println ("Current Property type name:" +cla.getname ()); Object Val=Typetransfer (Cla.getname (), attrValue); String MethodName= "Set" + attrname.substring (0, 1). toUpperCase () + attrname.substring (1); System.out.println ("Current node property corresponds to Method name:" +methodName); Method Method=Clazz.getdeclaredmethod (MethodName, CLA);        Method.invoke (user, Val); }        //Get child nodeslist<element> elements =rootelement.elements ();  for(Element element:elements) {//root Node A section nameString elemname =Element.getname (); System.out.println ("Current node Property name:" +elemname); //root node value of a nodeString Elemvalue =Element.gettext (); //setter method for locating corresponding attribute in user object by node nameField filed =Clazz.getdeclaredfield (elemname); Class CLA= Filed.gettype ();//Property TypeSystem.out.println ("Current Property type name:" +cla.getname ()); Object Val=Typetransfer (Cla.getname (), elemvalue); String MethodName= "Set" + elemname.substring (0, 1). toUpperCase () + elemname.substring (1); System.out.println ("Current node property corresponds to Method name:" +methodName); Method Method=Clazz.getdeclaredmethod (MethodName, CLA);        Method.invoke (user, Val);    } System.out.println (user); }    Private StaticObject Typetransfer (String typeName, String Vlaue)throwsParseException {Switch(typeName) { Case"Java.lang.Integer":            returninteger.valueof (Vlaue);  Case"Java.lang.String":            returnVlaue;  Case"Java.util.Date":            return NewSimpleDateFormat ("Yyyy-mm-dd"). Parse (Vlaue); }        return NULL; }}

Analysis of how to parse XML document into Java object (to be sorted)

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.