xstream实现xml与Object之间的自如转换

  • 因为项目中用到xml交互数据,所以一时间想起了这个工具(XStream),于是琢磨了怎么使用。通过自已的一些编程经验及网络的强大,最终轻松搞定。需要准备如下jar包(xstream-1.4.7.jar、xpp3_min-1.1.4c.jar、xmlpull-1.1.3.1.jar)

    1. 编写对应实体

  • RootBean

    @XStreamAlias("root")
    public class RootBean {
        @XStreamAlias("DocInfo")
        private DocInfoBean docInfo;
        @XStreamAlias("PAGES")
        private List pages;

        //getter和setter
        ...
    }
  • DocInfoBean
public class DocInfoBean {
    @XStreamAlias("APP_CODE")
    private String appCode;
    @XStreamAlias("APP_NAME")
    private String appName;
    @XStreamAlias("BUSI_NO")
    private String busiNo;
    //getter和setter
        ...
}
  • PageBean
@XStreamAlias("PAGE")
public class PageBean {
    @XStreamAlias("DOC_TYPE")
    @XStreamAsAttribute
    private String docType;
    @XStreamAlias("DOC_NAME")
    @XStreamAsAttribute
    private String docName;
    @XStreamAlias("PAGE_URL")
    @XStreamAsAttribute
    private String pageUrl;
    @XStreamAlias("PAGE_NAME")
    @XStreamAsAttribute
    private String pageName;
    @XStreamAlias("UP_USER")
    @XStreamAsAttribute
    private String upUser;
    @XStreamAlias("UP_ORG")
    @XStreamAsAttribute
    private String upOrg;
    @XStreamAlias("UP_TIME")
    @XStreamAsAttribute
    private String upTime;
    //getter和setter
        ...
}

2. xml到对象

private RootBean getRootBean(File xmlFile)  {
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(busiXmlFile), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        XStream xStream=new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xStream.alias("root", RootBean.class);
        xStream.autodetectAnnotations(true);
        return (RootBean) xStream.fromXML(reader);
    }

2. 对象到xml

public void testObj2XmlNew(){
        RootBean rootBean = new RootBean();
        DocInfoBean docInfo = new DocInfoBean();
        docInfo.setAppCode("009");
        docInfo.setAppName("测试");
        docInfo.setBusiNo("12345a");

        rootBean.setDocInfo(docInfo);
        List pages = new ArrayList();
        for(int i=0;i<3;i++){
            PageBean page = new PageBean();
            page.setDocName("身份证");
            page.setDocType("010");
            page.setPageName("0000.jpg");
            page.setPageUrl("1111.jpg");
            page.setUpOrg("惠普");
            page.setUpTime("34543312321");
            page.setUpUser("12433124");
            pages.add(page);
        }
        rootBean.setPages(pages);

        //解决了早期bug("_"转化到xml时会变为"__")
        //XStream xStream=new XStream(new DomDriver());        
        XStream xStream=new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xStream.alias("root", RootBean.class);
        xStream.autodetectAnnotations(true);
        String str= xStream.toXML(rootBean);
        System.out.println(str);
    }
  • 运行结果
<root>
  <DocInfo>
    <APP_CODE>009APP_CODE>
    <APP_NAME>测试APP_NAME>
    <BUSI_NO>12345aBUSI_NO>
  DocInfo>
  <PAGES>
    <PAGE DOC_TYPE="010" DOC_NAME="身份证" PAGE_URL="1111.jpg" PAGE_NAME="0000.jpg" UP_USER="12433124" UP_ORG="惠普" UP_TIME="34543312321"/>
    <PAGE DOC_TYPE="010" DOC_NAME="身份证" PAGE_URL="1111.jpg" PAGE_NAME="0000.jpg" UP_USER="12433124" UP_ORG="惠普" UP_TIME="34543312321"/>
    <PAGE DOC_TYPE="010" DOC_NAME="身份证" PAGE_URL="1111.jpg" PAGE_NAME="0000.jpg" UP_USER="12433124" UP_ORG="惠普" UP_TIME="34543312321"/>
  PAGES>
root>

你可能感兴趣的:(开发技巧汇总,XStream,xml,object,注解)