Jaxb多层 java 与 xml 转换

  1. /** 
  2.  *  
  3.  */  
  4. package com.wonders.quartz.cocc.model.xml;  
  5.   
  6. import java.util.List;  
  7.   
  8. import javax.xml.bind.annotation.XmlAttribute;  
  9. import javax.xml.bind.annotation.XmlElement;  
  10. import javax.xml.bind.annotation.XmlRootElement;  
  11.   
  12. import com.wonders.quartz.cocc.model.vo.CoccListVo;  
  13. import com.wonders.quartz.cocc.model.vo.CoccReportVo;  
  14.   
  15. /**  
  16.  * @ClassName: CoccReportXml  
  17.  * @Description: TODO(这里用一句话描述这个类的作用)  
  18.  * @author zhoushun  
  19.  * @date 2013年12月10日 下午2:31:33  
  20.  *   
  21.  */  
  22. @XmlRootElement(name = "root")  
  23. public class CoccReportXml {  
  24.     @XmlAttribute(name = "type")  
  25.     public String type="coccMetroWeekReport";  
  26.     @XmlAttribute(name = "date")  
  27.     public String date= new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());  
  28.     @XmlElement(name = "list")  
  29.     public CoccListVo list;  
  30.   
  31.       
  32.       
  33. }  


  1. /** 
  2.  *  
  3.  */  
  4. package com.wonders.quartz.cocc.model.vo;  
  5.   
  6. import java.util.List;  
  7.   
  8. import javax.xml.bind.annotation.XmlAccessType;  
  9. import javax.xml.bind.annotation.XmlAccessorType;  
  10. import javax.xml.bind.annotation.XmlElement;  
  11. import javax.xml.bind.annotation.XmlRootElement;  
  12.   
  13. /**  
  14.  * @ClassName: CoccReport  
  15.  * @Description: TODO(这里用一句话描述这个类的作用)  
  16.  * @author zhoushun  
  17.  * @date 2013年12月10日 下午2:12:33  
  18.  *   
  19.  */  
  20. @XmlRootElement(name = "bbbb")  
  21. @XmlAccessorType(XmlAccessType.FIELD)  
  22. public class CoccListVo {  
  23.     @XmlElement(name = "cocc")  
  24.     public List<CoccReportVo> list;  
  25.       
  26. }  


  1. /** 
  2.  *  
  3.  */  
  4. package com.wonders.quartz.cocc.model.vo;  
  5.   
  6. import javax.xml.bind.annotation.XmlAccessType;  
  7. import javax.xml.bind.annotation.XmlAccessorType;  
  8. import javax.xml.bind.annotation.XmlRootElement;  
  9.   
  10. /**  
  11.  * @ClassName: CoccReport  
  12.  * @Description: TODO(这里用一句话描述这个类的作用)  
  13.  * @author zhoushun  
  14.  * @date 2013年12月10日 下午2:12:33  
  15.  *   
  16.  */  
  17. @XmlRootElement(name = "123")  
  18. @XmlAccessorType(XmlAccessType.FIELD)  
  19. public class CoccReportVo {  
  20.     public String id;  
  21.     public String title;  
  22.     public String source;  
  23.     public String createTime;  
  24.     public String publishTime;  
  25.     public String url;  
  26.     public String getId() {  
  27.         return id;  
  28.     }  
  29.     public void setId(String id) {  
  30.         this.id = id;  
  31.     }  
  32.     public String getTitle() {  
  33.         return title;  
  34.     }  
  35.     public void setTitle(String title) {  
  36.         this.title = title;  
  37.     }  
  38.     public String getSource() {  
  39.         return source;  
  40.     }  
  41.     public void setSource(String source) {  
  42.         this.source = source;  
  43.     }  
  44.     public String getCreateTime() {  
  45.         return createTime;  
  46.     }  
  47.     public void setCreateTime(String createTime) {  
  48.         this.createTime = createTime;  
  49.     }  
  50.     public String getPublishTime() {  
  51.         return publishTime;  
  52.     }  
  53.     public void setPublishTime(String publishTime) {  
  54.         this.publishTime = publishTime;  
  55.     }  
  56.     public String getUrl() {  
  57.         return url;  
  58.     }  
  59.     public void setUrl(String url) {  
  60.         this.url = url;  
  61.     }  
  62.       
  63.       
  64. }  


  1. try{  
  2.             StringWriter writer = new StringWriter();  
  3.             JAXBContext context = JAXBContext.newInstance(CoccReportXml.class);  
  4.             Marshaller m = context.createMarshaller();  
  5.             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   
  6.             m.setProperty(Marshaller.JAXB_ENCODING, "GBK"); //防止文件中文乱码  
  7.             m.marshal(xml, writer);  
  8.             result = writer.toString();  
  9.         }catch(Exception e){  
  10.             result = "none";  
  11.         }  


最终显示xml字符串的节点名称 以外部包含该类的节点上的名称为准。
XML:
  1. <?xml version="1.0" encoding="GBK" standalone="yes"?>  
  2. <root date="2014-01-13" type="coccMetroWeekReport">  
  3.     <list>  
  4.         <cocc>  
  5.             <id>54844</id>  
  6.             <title>上海轨道交通网络运营生产周报(2014年1月6日—2014年1月12日)</title>  
  7.             <source>COCC</source>  
  8.             <createTime>2014-01-13</createTime>  
  9.             <publishTime>2014-01-13</publishTime>  
  10.             <url>http://10.1.44.18/stfb/node393/node1254/201401/con1050155.htm</url>  
  11.         </cocc>  
  12.     </list>  
  13. </root> 


转载自:http://blog.csdn.net/z69183787/article/details/18262385

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值