我用的是XStream框架
这个框架真是好用,完美的将java对象和xml进行了转换。
首先就是创建要解析的xml对象的框架,对每一层封装成一个对象。这里面有一个比较重要的@XStreamAlias这个属性就是别名。
xml格式如下:
@XStreamAlias("Message") public class Message { <span style="white-space:pre"> </span>@XStreamAlias("Header") <span style="white-space:pre"> </span>private Header header; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@XStreamAlias("Body") <span style="white-space:pre"> </span>private Body body; <span style="white-space:pre"> </span>public Header getHeader() { <span style="white-space:pre"> </span>return header; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setHeader(Header header) { <span style="white-space:pre"> </span>this.header = header; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public Body getBody() { <span style="white-space:pre"> </span>return body; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setBody(Body body) { <span style="white-space:pre"> </span>this.body = body; <span style="white-space:pre"> </span>} }
@XStreamAlias("Header") public class Header { <span style="white-space:pre"> </span>@XStreamAlias("TransactionID") <span style="white-space:pre"> </span>private String transactionID; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public String getTransactionID() { <span style="white-space:pre"> </span>return transactionID; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setTransactionID(String transactionID) { <span style="white-space:pre"> </span>this.transactionID = transactionID; <span style="white-space:pre"> </span>} }
@XStreamAlias("Body") public class Body { <span style="white-space:pre"> </span>@XStreamAlias("UserInfo") <span style="white-space:pre"> </span>private UserInfo userInfo; <span style="white-space:pre"> </span>public UserInfo getUserInfo() { <span style="white-space:pre"> </span>return userInfo; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setUserInfo(UserInfo userInfo) { <span style="white-space:pre"> </span>this.userInfo = userInfo; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> }
@XStreamAlias("UserInfo") public class UserInfo { <span style="white-space:pre"> </span>@XStreamAlias("phonenum") <span style="white-space:pre"> </span>private String phonenum; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@XStreamAlias("username") <span style="white-space:pre"> </span>private String username; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@XStreamAlias("password") <span style="white-space:pre"> </span>private String password; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public String getPhonenum() { <span style="white-space:pre"> </span>return phonenum; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setPhonenum(String phonenum) { <span style="white-space:pre"> </span>this.phonenum = phonenum; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getUsername() { <span style="white-space:pre"> </span>return username; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setUsername(String username) { <span style="white-space:pre"> </span>this.username = username; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getPassword() { <span style="white-space:pre"> </span>return password; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setPassword(String password) { <span style="white-space:pre"> </span>this.password = password; <span style="white-space:pre"> </span>} }
<message>
<Header>
<transactionID>123456</transactionID>
</Header>
<Body>
<userInfo phonenum="1111" username="aaa" password="1111"><userInfo/>
</Body>
</message>
这里是首先要做的工作就是对要解析的xml的封装格式的对象化。
接下来就是解析
private BroadcastData parseEpgFile(File file) { BroadcastData castData = null; String fileName = file.getName(); InputStream inputStream = null; try { inputStream = new FileInputStream(file); // 用此种构造方法是为了去掉报文中下划线占两个字符的问题 XStream xstream = new XStream(new XppDriver( new XmlFriendlyReplacer("-_","_"))); xstream.autodetectAnnotations(true);//<span style="color: rgb(51,51,51); font-family: 'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif; font-size: 16px; line-height: 28.799999237060547px;">告诉XStream去解析JAVA bean中的annotation</span> xstream.processAnnotations(Message.class); castData = (Message) xstream.fromXML(inputStream); logger.info("read epg xml successful. fileName=" + fileName); } catch (Exception e) { logger.warn("error parsing epg file: " + file.getAbsolutePath(),e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { logger.error( "close inputStream ioException when in readEpgFile",e); } } } return castData; }
tream.autodetectAnnotations(true); , 这句代码的意思是告诉XStream对象需要自动识别annotation, 这在序列化(JAVA bean-->XML)的时候没什么问题。但是在反序列化的时候就有问题了,原因官网上说的比较模糊,总之就是不行,只能通过xstream.processAnnotations(Class clazz) 来显式的注册需要使用annotation的类才行,如果JAVA bean很多就会比较麻烦。但一般来说JAVA bean在代码组织结构中都比较集中,如放在听一个package下,这样也好办,可以再程序中将该package下的JAVA bean都获取,然后使用xstream.processAnnotations(Class[] clazzs) 批量注册。
如果xml含有list对象,例如:‘
<message>
<Header>
<transactionID>123456</transactionID>
</Header>
<Body>
<userInfo phonenum="1111" username="aaa" password="1111"><userInfo/>
<userInfo phonenum="1111" username="aaa" password="1111"><userInfo/>
</Body>
</message>
则使用:@XStreamAlias("Body") public class List { @XStreamImplicit(itemFieldName="<span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>") private java.util.List<<span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>> <span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>; public java.util.List<<span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>> getProductInfo() { return <span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>; } public void setProductInfos(java.util.List<<span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>) { this.<span style="color: rgb(51,sans-serif; font-size: 16px; line-height: 28.799999237060547px; white-space: pre;">userInfo</span>= <span style="font-family: 'Helvetica Neue',sans-serif;">userInfo</span>; } }@XStreamImplicit(itemFieldName=" userInfo ")这个注解就是反序列化list的
对于最里面的对象的属性值,需要注意一点就是要添加@XStreamAsAtribute这个注解,否则范序列化后,获取不到想要的属性值。
@XStreamAlias("ProductInfo") public class ProductInfo { @XStreamAsAttribute @XStreamAlias("command") private String command; @XStreamAsAttribute @XStreamAlias("productid") private String productid; @XStreamAsAttribute @XStreamAlias("endtime") private String endtime; public String getCommand() { return command; } public void setCommand(String command) { this.command = command; } public String getProductid() { return productid; } public void setProductid(String productid) { this.productid = productid; } @Override public String toString() { return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE); } }原文链接:https://www.f2er.com/xml/298146.html