日期:2014-05-20  浏览次数:20814 次

求大神路过,JAXB怎样处理动态集合啊?
就是当我用JAXB转xml字符串为java对象时,里面包含有集合对象。但是集合对象中的类型又不固定。

  我想用java的多态来实现,但是好像JAXB并不能识别。

  简单核心代码如下:

Java code

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
        
    import com.keer.common.domain.ListBean;
    import com.keer.common.domain.Person;
    
    public class Test{
        
        private JdbcTemplate jdbcTemplate;
        
        //这是Test类型
        public String getAc01All() {
            String sql ="select aac001,aac002,aac003 from ac01";
            List<Person> list = jdbcTemplate.query(sql, new RowMapper<Person>(){
                public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Person p = new Person();
                    p.setAac001(rs.getString("aac001"));
                    p.setAac002(rs.getString("aac002"));
                    p.setAac003(rs.getString("aac003"));
                    return p;
                }
            });
            ListBean<Person> lb = new ListBean<Person>();
            
            String s =list2XML(lb.getClass(),list);    
            
            System.out.println(s);
            return s;
        }


        public String list2XML(Class<?> cla,List<?> list,Class<?> clas){
            StringWriter writer = new StringWriter();
            ListBean lb =null;
            try {
                
                Object o=cla.newInstance();
                if(o instanceof ListBean){
                    lb = (ListBean)o;
                    lb.setList(list);
                }
                JAXBContext context = null;
                if(null != clas){
                    context = JAXBContext.newInstance(cla,clas);
                }else{
                    context = JAXBContext.newInstance(cla);
                }
                
                marshallXmlType(lb, writer);
            } catch (Exception e) {
                e.printStackTrace();
            }
              return writer.toString();
        }
        
        
        public static <T> void marshallXmlType(T value, Writer os) {  
            try {  
                Class<T> clzz = (Class<T>)value.getClass();  
                JAXBContext context = JAXBContext.newInstance(clzz);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
                m.setAdapter(ListAdapter.class,new ListAdapter<Person>());
                QName name = new QName(clzz.getSimpleName());
                JAXBElement<T> element = new JAXBElement<T>(name, clzz, value);  
                m.marshal(element, os);  
            } catch (JAXBException e) {  
                e.printStackTrace();  
            }
        }
        
        public static void main(String[] args) {
            System.out.println(new PersonInfo().getAc01All());
        }
    }

    //List模型类
    
    import java.util.List;

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

    @XmlRootElement(name = "ListBean")
    public class ListBean<T> {
        
        private String name;
        private List<T> list;
        
        @XmlJavaTypeAdapter(ListAdapter.class)
        public List<T> getList() {
            return  list;
        }
     
        public void setList(List<T> list) {
            this.list = list;
        }
        
        @XmlAttribute
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
    }

    //模型类

    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 javax.xml.bind.annotation.XmlType;
    
    @XmlRootElement(name = "Person")
    public class Person extends Domain {

        @XmlElement(required = true)
        protected String aac001;
        @XmlElement(required = true)
        protected String aac002;
        @XmlElement(required = true)
        protected String aac003;
    }
    
    //xml2集合转换类
    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class ListAdapter<T extends Domain> extends XmlAdapter<Domain [], List<Domain>> {

        public T [] marshal(List<Domain> domains) throws Exception {
            Domain [] dos = new Domain[domains.size()];
            int i = 0;
            for (Domain domain : domains) {
                dos[i++] = domain;
                System.out.println(domain.getClass().getSimpleName());
            }
            return (T[]) dos;
        }

        public List<Domain> unmarshal(Domain [] paramValueType) throws Exception {
            List<Domain> list = new ArrayList<Domain>();
            for (Domain domain : list) {
                list.add(domain);
            }
            return list;
        }
    }
    
    //抽象模型类
    
    import java.io.Serializable;

    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name = "Domain")
    public class Domain implements Serializable {
        private static final long serialVersionUID = -3851152784887793734L;
    }