Xstream是一个功能比较强大的xml和java对象互转的工具包。可以通过简单的api调用,便实现一系列的转换操作。大家都知道微信的接口调用都是通过xml的形式进行数据交互的,所以在一些传统的接口调用和微信开发过程当中尤其适用。

1.导入依赖

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.10</version>
</dependency>

2.使用工具类
//转化为xml

            stream.alias("ROOT",CarInfo.class);
            String carInfoXml = stream.toXML(carInfo);
            String strAlarmInfo="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"+carInfoXml;

转成以后为:

 String strAlarmInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" +
                "<ROOT>"+
                "<KKMY>2</KKMY>"+
                "<KKBH>3</KKBH>"+
                "<JGSK>20170311162801001</JGSK>"+
                "<CDBH>4</CDBH>"+
                "<HPHM>浙A12345</HPHM>"+
                "<HPYS>1</HPYS>"+
                "<HPZL>08</HPZL>"+
                "<CLSD>8</CLSD>"+
                "<CWKC>1</CWKC>"+
                "<CSYS>A</CSYS>"+
                "<CLLX>1</CLLX>"+
                "<WFLX>1625</WFLX>" +
                "<TPSL>1</ TPSL>" +
                "<CPTPLJ>http://127.0.0.1:8129/20140215_浙A12345_车牌.jpg<CPTPLJ>" +
                "<TPLJ1>http://127.0.0.1:8129/20140215_浙A12345_过车.jpg</TPLJ1>" +
                "<TPLJ2></TPLJ2>" +
                "<TPLJ3></TPLJ3>" +
                "<SSYF>1</SSYF>" +
                "</ROOT>";