package com.mooc.freemarker2dto; public class BaseDto { }
package com.mooc.freemarker2dto; public class Book extends BaseDto{ private String name; private String author; private String year; private float price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "Book [name=" + name + ", author=" + author + ", year=" + year + ", price=" + price + "]"; } }
package com.mooc.freemarker2dto; import java.util.ArrayList; import java.util.List; public class ResultDto { private String Count; private List<BaseDto> detailData = new ArrayList(); public String getCount() { return Count; } public void setCount(String count) { Count = count; } public List<BaseDto> getDetailData() { return detailData; } public void setDetailData(List<BaseDto> detailData) { this.detailData = detailData; } }
<?xml version="1.0" encoding="utf-8"?> <bookstore> <books count="2"> <book> <name>冰与火之歌</name> <author>乔治马丁</author> <year>2014</year> <price>89</price> </book> <book> <name>安徒生童话</name> <author>安徒生</author> <year>2014</year> <price>69</price> </book> </books> </bookstore>
package com.mooc.freemarker2dto; public class User extends BaseDto{ private String name; private char sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
package com.mooc.freemarker2dto; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class DtoUtils { public static ResultDto parserDto(String repsponseXml,BaseDto baseDto) throws Exception{ Document doc = DocumentHelper.parseText(repsponseXml); Element rootElement = doc.getRootElement(); ResultDto resultDto = new ResultDto(); for (Iterator<Element> i = rootElement.elementIterator(); i.hasNext();) { Element monits = (Element) i.next(); element2Dto(monits, resultDto,baseDto); } return resultDto; } public static void element2Dto(Element element,ResultDto resultDto,BaseDto baseDto) throws Exception{ List<Attribute> bean = new ArrayList<Attribute>(); bean = element.attributes(); if(bean.size()>0){ resultDto.setCount(bean.get(0).getStringValue()); } if(element.hasContent()){ List<Element> list = element.elements(); for (int i=0;i<list.size();i++){ BaseDto obj = baseDto.getClass().newInstance(); Element base = list.get(i); if(base.elements().size()>0){ BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for(PropertyDescriptor property : propertyDescriptors){ //获取属性名 String key = property.getName(); if(base.element(key)!=null){ Object value = base.element(key).getStringValue(); Method setter = property.getWriteMethod(); Class<?>[] params = setter.getParameterTypes(); if (params != null && params.length > 0 && value != null){ Class<?> param = params[0]; System.out.println("param:"+param.getName()+" value:"+value.getClass().getName()); if(param.getName().equals(value.getClass().getName())){ setter.invoke(obj, value); continue; } if (param.getName().equals(Integer.class.getName()) // Integer || param.getName().equals(Integer.TYPE.getName())) { value= Integer.valueOf(value.toString()); } else if (param.getName().equals(Double.class.getName()) // Double || param.getName().equals(Double.TYPE.getName())) { value= Double.valueOf(value.toString()); } else if (param.getName().equals(Float.class.getName()) // Float || param.getName().equals(Float.TYPE.getName())) { value= Float.valueOf(value.toString()); } else if (param.getName().equals(Long.class.getName()) // Long || param.getName().equals(Long.TYPE.getName())) { value= Long.valueOf(value.toString()); }else if (param.getName().equals(Character.class.getName())||param.getName().equals(Character.TYPE.getName())) { char c[] = value.toString().toCharArray(); if(c.length>0){ value = c[0]; } } setter.invoke(obj, value); } } } } resultDto.getDetailData().add(obj); } } } }
package com.mooc.freemarker2dto; import java.util.List; public class Test { public static void main(String[] args) throws Exception { String repsponseXml = "<?xml version='1.0' encoding='UTF-8'?><bookstore><books count='2'><book><name>冰与火之歌</name><author>乔治马丁</author><year>2014</year><price>89</price></book><book><name>安徒生童话</name><author>安徒生</author><year>2014</year><price>69</price></book></books></bookstore>"; String resp = "<?xml version='1.0' encoding='UTF-8'?><bookstore><users count='2'><user><name>小红</name><sex>1</sex><age>22</age></user><user><name>小明</name><sex>1</sex><age>19</age></user></users></bookstore>"; Book book = new Book(); ResultDto resultDto = DtoUtils.parserDto(repsponseXml, book); System.err.println(resultDto.getCount()); List<BaseDto> baseDtos = resultDto.getDetailData(); for (BaseDto baseDto : baseDtos) { Book book2 = (Book) baseDto; System.out.println(book2); } User user = new User(); ResultDto resultDtos = DtoUtils.parserDto(resp, user); System.out.println("数量:"+resultDtos.getCount()); List<BaseDto> baseDtoss = resultDtos.getDetailData(); for (BaseDto baseDto : baseDtoss) { User user2 = (User) baseDto; System.out.println(user2); } } }