XStream JavaBean example of converting XML into multiple object combinations

Source: Internet
Author: User

The following is an example of a javabean using XStream to convert XML data into multiple object combinations:

XML Message Format:

<?xml version= "1.0" encoding= "GBK" standalone= "no"?>
<B2CRes>
<interfaceName></interfaceName>
<interfaceVersion></interfaceVersion>
<orderInfo>
<orderDate></orderDate>
<curType></curType>
<merID></merID>
<subOrderInfoList>
<subOrderInfo>
<orderid></orderid>
<amount></amount>
<installmentTimes></installmentTimes>
<merAcct></merAcct>
<tranSerialNo></tranSerialNo>
</subOrderInfo>
......
<subOrderInfo>
<orderid></orderid>
<amount></amount>
<installmentTimes></installmentTimes>
<merAcct></merAcct>
<tranSerialNo></tranSerialNo>
</subOrderInfo>
</subOrderInfoList>
</orderInfo>
<custom>
<verifyJoinFlag></verifyJoinFlag>
<JoinFlag></JoinFlag>
<UserNum></UserNum>
</custom>
<bank>
<TranBatchNo></TranBatchNo>
<notifyDate></notifyDate>
<tranStat></tranStat>
<comment></comment>
</bank>
</B2CRes>


The following are the relevant Java classes for the corresponding XML:
public class Suborderinfo {
Private String orderid;//Payment order number
Private String amount;//Order Amount
Private String installmenttimes;//Installment period 1, 3, 6, 9, 12, 18, 24;1 on behalf of full payment
Private String meracct;//merchant account
Private String transerialno;//Bank instruction serial Number

... Get Set method

}


public class Custom {

Private String verifyjoinflag;//Inspection Joint logo
Private String joinflag;//Customer Joint logo
Private String usernum;//Joint membership number


... Get Set method

}

public class Bank {

Private String tranbatchno;//Lot number
Private String notifydate;//return notification date time YYYYMMDDHHMMSS
Private String transtat;//Order Processing status 1-transaction succeeded, liquidated 2-transaction failed 3-transaction suspicious
Private String comment;//Remarks

Get Set method

}

public class Suborderinfolist {

Private suborderinfo[] Suborderinfo; There are multiple suborderinfo detailed orders in the Suborderinfolist class

Get Set method

}


public class OrderInfo {
Private String orderDate;
Private String Curtype;
Private String Merid;
Private suborderinfo[] suborderinfolist; List<suborderinfo> suborderinfolist; This can also be written here

Get Set method

}

public class B2cres {
Private String InterfaceName;
Private String interfaceversion;
Private OrderInfo OrderInfo;
Private custom named Custom;
Private Bank bank;

Get Set method

}


Test class;

public class Testxstream {
public static void Main (string[] args) {
try {


        string data= "<?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>0000144</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> trading success. </comment></bank></B2CRes> ";
XStream XStream = new XStream (new Domdriver ());

This alias is set to recognize the alias of the class object node in the following XML, otherwise the class full name
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);
Reading from an XML file
B2cres b2cres= (b2cres) xstream.fromxml (New FileReader (New File ("d:/myworkspace/test/src/xstreamtest/b2cres/ B2cres22.xml "));
SYSTEM.OUT.PRINTLN ("Interface Name:" +b2cres.getinterfacename ());
OrderInfo Orderinfo=b2cres.getorderinfo ();
System.out.println ("Merchant account Number:" +orderinfo.getmerid ());
Suborderinfo suborderinfo=orderinfo.getsuborderinfolist () [0];
SYSTEM.OUT.PRINTLN ("Payment order ID:" +suborderinfo.getorderid ());
SYSTEM.OUT.PRINTLN ("Bank serial Number:" +suborderinfo.gettranserialno ());
Custom Custom=b2cres.getcustom ();
SYSTEM.OUT.PRINTLN ("Inspection Joint Logo:" +custom.getverifyjoinflag ());
Bank Bank=b2cres.getbank ();
System.out.println ("Remark:" +bank.getcomment ());
catch (Exception e) {
E.printstacktrace ();
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.