使用 XStream 来转换 xml,xml转为 Pojo(实体类),xml转为map
<dependency> <!-- xml与java pojo 映射类库 --> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.10</version> </dependency>
实现类:
import com.thoughtworks.xstream.XStream; /** * 使用 XStream 来转换 xml <---> Java Pojo * * @since: v1.0 */ public class XStreamFactory { /** * 将 xml 转成 T 类型对象 * * @param xml * @param clazz 要转换的类型,如果为 null 则返回 null * @return */ public final static <T> T fromXML(String xml, Class<T> clazz){ if(clazz != null && !xml.equals("")){ XStream xs = new XStream(); XStream.setupDefaultSecurity(xs); xs.ignoreUnknownElements(); xs.registerConverter(new MapEntryConverter()); xs.processAnnotations(clazz); xs.allowTypes(new Class[]{clazz}); return (T)xs.fromXML(xml); } return null; } /** * 将 xml 转成 T 类型对象。(xml的根节点名称与类名称不相同时使用本方法) * * @param alias xml 根节点名称(根节点名与类名不一致时) * @param xml * @param clazz * @return */ public final static <T> T fromXML(String alias, String xml, Class<T> clazz){ if(clazz != null && !xml.equals("")){ XStream xs = new XStream(); XStream.setupDefaultSecurity(xs); xs.allowTypes(new Class[]{clazz}); xs.ignoreUnknownElements(); xs.alias(alias, clazz); xs.registerConverter(new MapEntryConverter()); xs.autodetectAnnotations(true); return (T)xs.fromXML(xml); } return null; } }
例程:
PhoneDeviceInformation info = XStreamFactory.fromXML(xml, PhoneDeviceInformation.class);//xml转pojo
Map<String, Object> datas = XStreamFactory.fromXML("NetworkConfiguration", xml, Map.class);//xml转map
PhoneDeviceInformation xml:
<DeviceInformation> <serviceMode>On-premise</serviceMode> <serviceDomain/> <serviceState>Idle</serviceState> <MACAddress>CC5A535E0DF0</MACAddress> <HostName>SEPCC5A535E0DF0</HostName> </DeviceInformation>
NetworkConfiguration xml:
<NetworkConfiguration> <MACAddress>CC5A535E0DF0</MACAddress> <HostName>SEPCC5A535E0DF0</HostName> <ServiceMode>On-premise</ServiceMode> </NetworkConfiguration>
PhoneDeviceInformation.java
import com.thoughtworks.xstream.annotations.XStreamAlias; /** * * <p> 请求返回一个 xml 描述文件,这个 xml 描述文件映射成 pojo 就是本类 * * @since: v1.0 */ @XStreamAlias("DeviceInformation") public class PhoneDeviceInformation { private String serviceMode; private String serviceDomain; private String serviceState; @XStreamAlias("MACAddress") private String macAddress; @XStreamAlias("HostName") private String hostName; /** * @return the serviceMode */ public String getServiceMode() { return serviceMode; } /** * @param serviceMode the serviceMode to set */ public void setServiceMode(String serviceMode) { this.serviceMode = serviceMode; } /** * @return the serviceDomain */ public String getServiceDomain() { return serviceDomain; } /** * @param serviceDomain the serviceDomain to set */ public void setServiceDomain(String serviceDomain) { this.serviceDomain = serviceDomain; } /** * @return the serviceState */ public String getServiceState() { return serviceState; } /** * @param serviceState the serviceState to set */ public void setServiceState(String serviceState) { this.serviceState = serviceState; } /** * @return the macAddress */ public String getMacAddress() { return macAddress; } /** * @param macAddress the macAddress to set */ public void setMacAddress(String macAddress) { this.macAddress = macAddress; } /** * @return the hostName */ public String getHostName() { return hostName; } /** * @param hostName the hostName to set */ public void setHostName(String hostName) { this.hostName = hostName; } }