xstream xml转化成java对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xstream xml转化成java对象相关的知识,希望对你有一定的参考价值。
xml to java对象:static MessageDatabase load(String persistFile)
MessageDatabase messageDatabase = null;
Reader reader = null;
try
reader = new FileReader(persistFile);
XStream xstream = new XStream(new JDomDriver());
messageDatabase = (MessageDatabase) xstream.fromXML(reader);
// for upgrade
if (messageDatabase != null)
messageDatabase.upgrade();
catch (FileNotFoundException ex)
;
finally
if (reader != null)
try
reader.close();
catch (IOException ex)
log.warn("Exception when closing reader", ex);
if (messageDatabase == null)
messageDatabase = new MessageDatabase();
messageDatabase.setMessages(new LinkedList());
messageDatabase.setStickyMessages(new LinkedList());
messageDatabase.persistFile = persistFile;
return messageDatabase;
参考技术A http://chen-rojer-gmail-com.iteye.com/blog/1435254/
XStream将XML转换为JAVA对象快速指南
XStream是一套简单实用的JAVA类库,它用于序列化对象和XML对象之间互相转换。由于XStream配置简单,灵活易用,因此在XML对象和JAVA对象序列化市场上有很大的空间。本文并不会对XStream的特性做详细介绍,只是以实例的方式演示XStream是多么的容易上手。
1.XStream官网下载最新版的Jar包,上传到工程路径。
2.假设我们要将如下的XML对象转换为JAVA对象。
<?xml version="1.0" encoding="UTF-8"?> <order> <orderId>201612150001</orderId> <orderPrice>40.23</orderPrice> <createDate>20161215180000</createDate> <product> <productName>lvrouhuoshao</productName> <productPrice>23</productPrice> </product> <product> <productName>鸡蛋灌饼</productName> <productPrice>17.23</productPrice> </product> </order>
3.根据XML结构构建订单对象和产品对象
HiOrder.java
package com.favccxx.favsoft.pojo; import java.util.List; public class HiOrder { private String orderId; private double orderPrice; private List<HiProduct> product; private String createDate; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public double getOrderPrice() { return orderPrice; } public void setOrderPrice(double orderPrice) { this.orderPrice = orderPrice; } public List<HiProduct> getProduct() { return product; } public void setProduct(List<HiProduct> product) { this.product = product; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } }
Product.java
package com.favccxx.favsoft.pojo; public class HiProduct { private String productName; private double productPrice; public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public double getProductPrice() { return productPrice; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } }
4.测试代码
package com.favccxx.favsoft.main; import com.favccxx.favsoft.pojo.HiOrder; import com.favccxx.favsoft.pojo.HiProduct; import com.thoughtworks.xstream.XStream; public class MainOrder { public static void main(String[] args) { String xmlbody = "<?xml version=‘1.0‘ encoding=‘UTF-8‘?><order><orderId>201612150001</orderId><orderPrice>40.23</orderPrice><createDate>2016-12-12 15:16:04</createDate><product><productName>lvrouhuoshao</productName><productPrice>23</productPrice></product><product><productName>鸡蛋灌饼</productName><productPrice>17.23</productPrice></product></order>"; XStream xStream = new XStream(); xStream.alias("order", HiOrder.class); xStream.alias("product", HiProduct.class); xStream.addImplicitCollection(HiOrder.class, "product"); HiOrder order = (HiOrder) xStream.fromXML(xmlbody); System.out.println(order.getOrderId()); } }
5.运行上面测试类,输出订单的详细信息。只需简单的几步就可以将XML对象转换为JAVA对象,你看到这,是不是心痒的想要上手试一试呢?
本文出自 “这个人的IT世界” 博客,请务必保留此出处http://favccxx.blog.51cto.com/2890523/1884453
以上是关于xstream xml转化成java对象的主要内容,如果未能解决你的问题,请参考以下文章