XStream对象组合的转换xml转换为Javabean:
例子1:
XML格式:
<?xml version="1.0" encoding="GBK" standalone="no" ?>
<B2CRes><interfaceName>ICBC_PERBANK_B2C</interfaceName>
<interfaceVersion>1.0.0.11</interfaceVersion>
<orderInfo>
<orderDate>20100901162142</orderDate>
<curType>001</curType>
<merID>0200EC20001119</merID>
<subOrderInfoList>
<subOrderInfo>
<orderid>201009011621420</orderid>
<amount>100</amount>
<installmentTimes>1</installmentTimes>
<merAcct>0200026009018372212</merAcct>
<tranSerialNo>HFG000000000000421</tranSerialNo>
</subOrderInfo>
<subOrderInfo>
<orderid>201009011621421</orderid>
<amount>120</amount>
<installmentTimes>1</installmentTimes>
<merAcct>0200026009018372212</merAcct>
<tranSerialNo>HFG000000000000422</tranSerialNo>
</subOrderInfo>
</subOrderInfoList>
</orderInfo>
<custom>
<verifyJoinFlag>0</verifyJoinFlag>
<JoinFlag></JoinFlag>
<UserNum></UserNum>
</custom>
<bank>
<TranBatchNo>000000000000098</TranBatchNo>
<notifyDate>20100910174050</notifyDate>
<tranStat>1</tranStat>
<comment>交易成功!</comment>
</bank>
</B2CRes>
对应的Javabean类:
package XStreamtest.b2cres;
public class B2CRes {
private String interfaceName;
private String interfaceVersion;
private OrderInfo orderInfo;
private Custom custom;
private Bank bank;
//get set方法
}
public class OrderInfo {
private String orderDate;
private String curType;
private String merID;
private SubOrderInfo[] subOrderInfoList; //List<SubOrderInfo> subOrderInfoList; 此处也可以这样写
// //get set方法
}
public class SubOrderInfo {
private String orderid;
private String amount;
private String installmentTimes;
private String merAcct;
private String tranSerialNo;
// //get set方法
测试Java类:
package XStreamtest.b2cres;
import java.io.File;
import java.io.FileReader;
import XStreamtest.vo.Customer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class TestXStream {
public static void main(String[] args) {
try {
String data=“上面的xml字符串”;
XStream xstream = new XStream(new DomDriver());
//设置了这个别名才能识别下面 xml 中的 product 节点,否则要用类全限名称 注意只设置对象的别名即可 属性不用设
xstream.alias("B2CRes", B2CRes.class);
xstream.alias("orderInfo", OrderInfo.class);
xstream.alias("subOrderInfo", SubOrderInfo.class);
xstream.alias("custom", Custom.class);
xstream.alias("bank", Bank.class);
B2CRes b2cres=(B2CRes)xstream.fromXML(data);
//从XML文件读取
//B2CRes b2cres=(B2CRes)xstream.fromXML(new FileReader(new File("D:/myworkspace/test/src/XStreamtest/b2cres/B2CRes22.xml")) );
System.out.println("接口名称:"+b2cres.getInterfaceName());
OrderInfo orderInfo=b2cres.getOrderInfo();
System.out.println("商户账号:"+orderInfo.getMerID());
SubOrderInfo subOrderInfo=orderInfo.getSubOrderInfoList()[0];
System.out.println("支付订单ID:"+subOrderInfo.getOrderid());
System.out.println("银行序列号:"+subOrderInfo.getTranSerialNo());
Custom custom=b2cres.getCustom();
System.out.println("检验联名标志:"+custom.getVerifyJoinFlag());
Bank bank=b2cres.getBank();
System.out.println("备注:"+bank.getComment());
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子2
xml格式:
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<Customer>
<custId>123</custId>
<custName>姓名</custName>
<orders>
<Order>
<orderId>0000212</orderId>
<orderName>订单名称</orderName>
<products>
<Product>
<prodId>1</prodId>
<prodName>产品名称</prodName>
<prodPrice>2.23</prodPrice>
</Product>
<Product>
<prodId>1</prodId>
<prodName>产品名称</prodName>
<prodPrice>2.23</prodPrice>
</Product>
</products>
</Order>
</orders>
</Customer>
对应的Javabean类:
public class Customer {
private int custId;
private String custName;
private List<Order> orders;
//get set 方法
}
public class Order {
private int orderId;
private String orderName;
private Product[] products;
//get set 方法
}
public class Product {private int prodId;
private String prodName;
private double prodPrice;
//get set 方法
}
测试类测试方法:
public void xmltobean(){
String data="上面的xml串";
XStream xstream = new XStream(new DomDriver());
//设置了这个别名才能识别下面 xml 中的 product 节点,否则要用类全限名称
xstream.alias("Customer", Customer.class);
xstream.alias("Order", Order.class);
xstream.alias("Product", Product.class);
Customer prod = (Customer)xstream.fromXML(data.toString());
/*其他重载方法它们能从不同的输入源,可指定根节点来构建对象
Object fromXML(InputStream input);
Object fromXML(InputStream xml, Object root);
Object fromXML(Reader xml);
Object fromXML(Reader xml, Object root); //B2CRes b2cres=(B2CRes)xstream.fromXML(new FileReader(new File("D:/myworkspace/test/src/XStreamtest/b2cres/B2CRes22.xml")) );
Object fromXML(String xml);
Object fromXML(String xml, Object root);
* */
System.out.println(prod.getCustName());
}