Generate xml files through class reflection
Entry Test class:
Note:There are many methods for class to look at java APIs
Package priv. yingmm. main; import java. io. byteArrayInputStream; import java. io. fileOutputStream; import java. lang. reflect. field; import java. util. arrayList; import java. util. list; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. transform. transformer; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import priv. yingmm. entity. area;/*** @ Description: xml generation test class * @ ClassName: reflexTest * @ author yingmm * @ date 2015-5-2 08:53:21 */public class ReflexTest {public static void main (String [] args) throws Exception {/** S test data */List
AreaList = new ArrayList (); for (int I = 0; I <10; I ++) {areaList. add (new Area (33022 + I, Ningbo, 3000);}/** E test data * // ** S generate xml and write data */DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); DocumentBuilder db = dbf. newDocumentBuilder (); String parseStr = ReflexTest. parseStr (areaList); Document document = db. parse (new ByteArrayInputStream (parseStr. getBytes (); TransformerFactory tf = TransformerFactory. newInstance (); Transformer t = tf. newTransformer (); t. setOutputProperty (encoding, UTF-8); DOMSource doms = new DOMSource (document. getDocumentElement (); StreamResult sr = new StreamResult (new FileOutputStream (area. xml); t. transform (doms, sr ); /** E generate xml and write data */}/*** @ param collection concatenated into a String * @ author yingmm * @ throws Exception */private static String parseStr (List list List) throws Exception {// TODO Auto-generated method stub StringBuffer sb = new StringBuffer (); sb. append (); for (int I = 0; I <list. size (); I ++) {@ SuppressWarnings (rawtypes) Class cl = list. get (I ). getClass (); Field [] f = cl. getDeclaredFields (); sb. append (); for (int j = 0; j <f. length; j ++) {if (areaCode. equals (f [j]. getName () {continue;} f [j]. setAccessible (true); sb. append (<+ f [j]. getName () +>); sb. append (f [j]. get (list. get (I); sb. append ( ); F [j]. setAccessible (false);} sb. append ();} sb. append (); Return sb. toString () ;}} Area. java class
Package priv. yingmm. entity;/*** @ Description: Region class * @ ClassName: Area * @ author yingmm * @ date 08:05:53 **/public class Area {private String areaCode; private String areaName; private int personNum; // private String city; public Area () {super ();} public Area (String areaCode, String areaName, int personNum) {super (); this. areaCode = areaCode; this. areaName = areaName; this. personNum = personNum;} public String getAreaCode () {return areaCode;} public void setAreaCode (String areaCode) {this. areaCode = areaCode;} public String getAreaName () {return areaName;} public void setAreaName (String areaName) {this. areaName = areaName;} public int getPersonNum () {return personNum;} public void setPersonNum (int personNum) {this. personNum = personNum ;}}