使用XStream,XMLSerializer 解析及格式转换
博主原创,转载请注明出处
1.引入对应的maven依赖:
<!--xstream--> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.9</version> </dependency>
2.解析示例:
如下xml:
1. <?xml version="1.0" encoding="utf-8"?> 2. <MbfService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 3. <input1> 4. <MbfHeader> 5. <ServiceCode>SyncSystemInfoMgmt</ServiceCode> 6. <Operation>syncSystemInfo</Operation> 7. <AppCode>ITSM</AppCode> 8. <UId>1d9ea071-889d-4f5a-8518-3dc9f77eaa01</UId> 9. <AuthId/> 10. </MbfHeader> 11. <MbfBody> 12. <systemInfo> 13. <action>A</action> 14. <systemNum>SYS0008001</systemNum> 15. <systemFcname>下载测试</systemFcname> 16. <systemFename>dadf</systemFename> 17. <systemFcenter>技术管理办公室</systemFcenter> 18. <systemScenter>总经办</systemScenter> 24. <systemDes>ITP新增应用系统</systemDes> 35. </systemInfo> 36. </MbfBody> 37. </input1> 38. </MbfService>
使用 @XStreamAlias 注解将xml解析为对应的实体类,解析转换对应java实体类为:
import lombok.Getter; import lombok.Setter; @Setter @Getter @XStreamAlias("MbfService") public class SyncSystemInfoData { private String version = "1.0.0"; @XStreamAlias("input1") private MbfService input1; @Setter @Getter public static class MbfService { @XStreamAlias("MbfHeader") private MbfHeader MbfHeader; @XStreamAlias("MbfBody") private MbfBody MbfBody; } @Setter @Getter public static class MbfHeader { @XStreamAlias("ServiceCode") private String ServiceCode; @XStreamAlias("Operation") private String Operation; @XStreamAlias("AppCode") private String AppCode; @XStreamAlias("UId") private String UId; @XStreamAlias("AuthId") private String AuthId; } @Setter @Getter public static class MbfBody { @XStreamAlias("systemInfo") private GroupInfo systemInfo; } @Setter @Getter public static class GroupInfo { @XStreamAlias("action") private String action; @XStreamAlias("systemNum") private String systemNum; @XStreamAlias("systemFcname") private String systemFcname; } }
第三步转换:
String txt= "示例xml"; XStreamEx xStream = new XStreamEx(); xStream.processAnnotations(SyncSystemInfoData.class); SyncSystemInfoData data = (SyncSystemInfoData) xStream.fromXML(txt);
示例2:当xml中具有列表属性,即某个标签会有多个。
<?xml version="1.0" encoding="UTF-8"?> <domain-list> <domain-summary> <domain-id>171190</domain-id> <domain-name>a.example.com</domain-name> <service-type>web</service-type> <status>Deployed</status> <enabled>true</enabled> <cdn-service-status>true</cdn-service-status> </domain-summary> <domain-summary> <domain-id>172260</domain-id> <domain-name>b.example.com</domain-name> <service-type>web</service-type> <status>Deployed</status> <enabled>true</enabled> <cdn-service-status>true</cdn-service-status> </domain-summary> </domain-list>
2.使用
@XStreamImplicit,:: XStreamImplicit注解有两个属性:itemFieldName是指当前集合数据转换为xml元素时的 elementName;keyFieldName在集合元素为复杂对象时,会使用集合元素的成员变量名作为元素的elementName,当集合元素为 基本数据类型及String类型时,keyFieldName指定的值将作为元素的elementName。
@XStreamAlias
解析对应的java实体类:
@XStreamAlias("domain-list") @Setter @Getter public class DomainList { /** 域名summary列表 **/ @XStreamImplicit(itemFieldName = "domain-summary") private List<DomainSummary> domainSummaryLst; }
@Getter @Setter public class DomainSummary { /** 域名ID **/ @XStreamAlias("domain-id") private String domainId; /** 域名 **/ @XStreamAlias("domain-name") private String domainName; /** 类型 **/ @XStreamAlias("service-type") private String serviceType; /** cname **/ @XStreamAlias("cname") private String cname; /** 状态 **/ @XStreamAlias("status") private String status; /** CDN服务器状态 **/ @XStreamAlias("cdn-service-status") private String cdnServiceStatus; /** 禁用启用 **/ @XStreamAlias("enabled") private String enabled; }
进行转换:
// 定义并封装查询参数 XStream xStream = new XStream(new DomDriver()); xStream.processAnnotations(DomainList.class); WsDomainList domainList = (DomainList) xStream.fromXML(示例中的xml);
将java bean转换为xml。
DomainList domain = new DomainList(); XStream xStream = new XStream(); // 通过注解方式的,一定要有这句话
xStream.processAnnotations(domainConf.getClass());
String xmlCfg = xStream.toXML(wsDomainConf);
使用XMLSerializer进行解析:
import net.sf.json.JSON; import net.sf.json.xml.XMLSerializer; import com.alibaba.fastjson.JSONObject; XMLSerializer xmlSerializer = new XMLSerializer(); JSON resutStr = xmlSerializer.read(""); JSONObject domainJson = JSONObject.parseObject(resutStr.toString());