Custom class Library: Java transformation XML file conversion Pojo tool

Source: Internet
Author: User

First, when Java read the XML configuration file, always through the dom4j third-party library actively resolved, recently discovered that the JDK class library can be converted to Pojo XML.

Ii. Writing XML conversion tool class Xmlutils

Package Com.moy.demo.common.utils;import Javax.xml.bind.jaxbcontext;import javax.xml.bind.unmarshaller;import Java.io.inputstream;import Java.io.reader;import Java.io.StringReader;/** * [project]:cloud-demo-master <br/> * [email]:[email protected] <br/> * [Date]:2018/4/30 <br/> * [Description]: <br/> * * @author Yexiangyang*/ Public Abstract classXmlutils { Public Static<T> T xmlfiletoobject (String Xmlfilepath, class<t>clazz) {ClassLoader Contextclassloader=Thread.CurrentThread (). Getcontextclassloader (); Try(InputStream InputStream =Contextclassloader.getresourceasstream (Xmlfilepath)) {Jaxbcontext Context=jaxbcontext.newinstance (Clazz); Unmarshaller Unmarshaller=Context.createunmarshaller (); T result=(T) Unmarshaller.unmarshal (InputStream); returnresult; } Catch(Exception e) {Throw NewRuntimeException ("convert XML to POJO failure!", E); }    }     Public Static<T> T xmlstringtoobject (String xmlstring, class<t>clazz) {        Try(Reader reader =NewStringReader (xmlstring)) {Jaxbcontext Context=jaxbcontext.newinstance (Clazz); Unmarshaller Unmarshaller=Context.createunmarshaller (); T result=(T) Unmarshaller.unmarshal (reader); returnresult; } Catch(Exception e) {Throw NewRuntimeException ("Convert XML string to POJO failure!", E); }    }}

Second, the test

A, the first step: Define an XML configuration file such as: Citylist.xml

<?XML version= "1.0" encoding= "UTF-8"?><CC1= "0">    <DD1= "101020100"D2= "Shanghai"D3= "Shanghai"D4= "Shanghai"/>    <DD1= "101220101"D2= "Hefei"D3= "Hefei"D4= "Anhui"/>    <DD1= "101190101"D2= "Nanjing"D3= "Jiangshu"D4= "Jiangsu"/>    <DD1= "101010100"D2= "Beijing"D3= "Beijing"D4= "Beijing"/>    <DD1= "101270101"D2= "Chengdu"D3= "Chengdu"D4= "Sichuan"/></C>
View Code

B, Step two: Write the XML corresponding entity city and CityList, and add the corresponding annotations

Package Com.moy.demo.common.utils;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.xmlaccessortype;import Javax.xml.bind.annotation.xmlattribute;import javax.xml.bind.annotation.XmlRootElement;/** * [project]:cloud-demo-master <br/> * [email]:[email protected] <br/> * [Date]:2018/4/30 <br/> * [Description]: <br/> * * @author Yexiangyang*/@XmlRootElement (Name="D") @XmlAccessorType (Xmlaccesstype.field) Public classCity {@XmlAttribute (name="D1")    PrivateString Cityid; @XmlAttribute (Name="D2")    PrivateString CityName; @XmlAttribute (Name="D3")    PrivateString Citycode; @XmlAttribute (Name="D4")    PrivateString Province;  PublicString Getcityid () {returnCityid; }     Public voidSetcityid (String Cityid) { This. Cityid =Cityid; }     PublicString Getcityname () {returnCityName; }     Public voidsetcityname (String cityname) { This. CityName =CityName; }     PublicString Getcitycode () {returnCitycode; }     Public voidSetcitycode (String citycode) { This. Citycode =Citycode; }     PublicString getprovince () {returnProvince; }     Public voidsetprovince (String province) { This. Province =Province; } @Override PublicString toString () {return "city{"+"cityid= '"+ Cityid +'\ ''+", Cityname= '"+ CityName +'\ ''+", citycode= '"+ Citycode +'\ ''+", province= '"+ Province +'\ ''+'}'; }}
View Code
Package Com.moy.demo.common.utils;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.xmlaccessortype;import Javax.xml.bind.annotation.xmlelement;import Javax.xml.bind.annotation.xmlrootelement;import java.util.List;/** * [project]:cloud-demo-master <br/> * [email]:[email protected] <br/> * [Date]:2018/4/30 <br/> * [Description]: <br/> * * @author Yexiangyang*/@XmlRootElement (Name="C") @XmlAccessorType (Xmlaccesstype.field) Public classcitylist {@XmlElement (name="D")    PrivateList<city>CityList;  PublicList<city>getcitylist () {returnCityList; }     Public voidSetcitylist (list<city>citylist) {         This. CityList =CityList; } @Override PublicString toString () {return "citylist{"+"citylist="+ CityList +'}'; }}
View Code

C, write unit test xmlutilstest

Package Com.moy.demo.common.utils;import Org.junit.test;importStaticorg.junit.assert.*;/** [Project]:d emo-master <br/> * [email]:[email protected] <br/> * [DATE]:2018/5/2 <br/> * [Desc Ription]: <br/> * * @author Yexiangyang*/ Public classxmlutilstest {@Test Public voidXmlfiletoobject () {citylist citylist= Xmlutils.xmlfiletoobject ("Citylist.xml", CityList.class); System. out. println (CityList); } @Test Public voidXmlstringtoobject () {String xmlstring="<?xml version=\ "1.0\" encoding=\ "utf-8\"? >\n"+"<c c1=\ "0\" >\n"+"<d d1=\ "101020100\" d2=\ "Shanghai \" d3=\ "shanghai\" d4=\ "shanghai \"/>\n"+"<d d1=\ "101220101\" d2=\ "Hefei \" d3=\ "hefei\" d4=\ "Anhui \" />\n"+"<d d1=\ "101190101\" d2=\ "nanjing \" d3=\ "jiangshu\" d4=\ "Jiangsu \" />\n"+"<d d1=\ "101010100\" d2=\ "Beijing \" d3=\ "beijing\" d4=\ "Beijing \"/>\n"+"<d d1=\ "101270101\" d2=\ "Chengdu \" d3=\ "chengdu\" d4=\ "Sichuan \" />\n"+"</c>"; CityList CityList= Xmlutils.xmlstringtoobject (xmlstring, CityList.class); System. out. println (CityList); }}
View Code

  

Yexiangyang

[Email protected]

Custom class Library: Java transformation XML file conversion Pojo tool

Related Article

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.