XStream XMLファイルを生成し、別名を設定

8879 ワード

public class B2BPayToXml {

    public Head head;
    
    public Body body;

    public void setHead(Head head) {
        this.head = head;
    }

    public void setBody(Body body) {
        this.body = body;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: Head.java, v 0.1
		2014 9 24   10:01:57 Exp $ 
 */
public class Head {
    public String MerPtcId;

    public String TranTime;

    public String TranCode;

    public String TranDate;

    public Head(String merPtcId, String tranTime, String tranCode, String tranDate) {
        MerPtcId = merPtcId;
        TranTime = tranTime;
        TranCode = tranCode;
        TranDate = tranDate;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: Body.java, v 0.1
		2014 9 24   10:03:30 Exp $ 
 */
public class Body {
    public String      MerTranSerialNo;

    public String      SafeReserved;
    
    // 
    public PtcInfo     ptcInfo;

    // 
    public BusiInfo    busiInfo;

    // 
    public UserInfo    userInfo;

    // 
    public GoodsInfo   goodsInfo;

    // 
    public TranInfo    tranInfo;

    // 
    public ChannelInfo channelInfo;

    // 
    public MemoInfo    memoInfo;

    public void setMerTranSerialNo(String merTranSerialNo) {
        MerTranSerialNo = merTranSerialNo;
    }

    public void setSafeReserved(String safeReserved) {
        SafeReserved = safeReserved;
    }

    public void setPtcInfo(PtcInfo ptcInfo) {
        this.ptcInfo = ptcInfo;
    }

    public void setBusiInfo(BusiInfo busiInfo) {
        this.busiInfo = busiInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setGoodsInfo(GoodsInfo goodsInfo) {
        this.goodsInfo = goodsInfo;
    }

    public void setTranInfo(TranInfo tranInfo) {
        this.tranInfo = tranInfo;
    }

    public void setChannelInfo(ChannelInfo channelInfo) {
        this.channelInfo = channelInfo;
    }

    public void setMemoInfo(MemoInfo memoInfo) {
        this.memoInfo = memoInfo;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: PtcInfo.java, v 0.1
		2014 9 24   10:09:52 Exp $ 
 */
public class PtcInfo {
    public String SubMerPtcId;

    public PtcInfo(String subMerPtcId) {
        SubMerPtcId = subMerPtcId;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: BusiInfo.java, v 0.1
		2014 9 24   10:08:00 Exp $ 
 */
public class BusiInfo {
    public String MerOrderNo;

    public BusiInfo(String merOrderNo) {
        MerOrderNo = merOrderNo;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: UserInfo.java, v 0.1
		2014 9 24   10:08:47 Exp $ 
 */
public class UserInfo {
    public String BuyerId;

    public String BuyerName;

    public String SellerId;

    public String SellerName;

    public UserInfo(String buyerId, String buyerName, String sellerId, String sellerName) {
        BuyerId = buyerId;
        BuyerName = buyerName;
        SellerId = sellerId;
        SellerName = sellerName;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: GoodsInfo.java, v 0.1
		2014 9 24   10:09:15 Exp $ 
 */
public class GoodsInfo {
    public String GoodsName;

    public String GoodsTxt;

    public String GoodsDesc;

    public GoodsInfo(String goodsName, String goodsTxt, String goodsDesc) {
        GoodsName = goodsName;
        GoodsTxt = goodsTxt;
        GoodsDesc = goodsDesc;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: TranInfo.java, v 0.1
		2014 9 24   10:10:17 Exp $ 
 */
public class TranInfo {
    public String TranModeId;

    public String TranAmt;

    public String TranCry;

    public TranInfo(String tranModeId, String tranAmt, String tranCry) {
        TranModeId = tranModeId;
        TranAmt = tranAmt;
        TranCry = tranCry;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: ChannelInfo.java, v 0.1
		2014 9 24   10:10:46 Exp $ 
 */
public class ChannelInfo {
    public String ChannelApi;

    public String ChannelInst;

    public ChannelInfo(String channelApi, String channelInst) {
        ChannelApi = channelApi;
        ChannelInst = channelInst;
    }
}
/** 
 *  
 * @author lizhiyong
 * @version $Id: MemoInfo.java, v 0.1
		2014 9 24   10:11:10 Exp $ 
 */
public class MemoInfo {
    public String BuyerMemo;

    public String SellerMemo;

    public String PlatMemo;

    public String PayMemo;

    public MemoInfo(String buyerMemo, String sellerMemo, String platMemo, String payMemo) {
        BuyerMemo = buyerMemo;
        SellerMemo = sellerMemo;
        PlatMemo = platMemo;
        PayMemo = payMemo;
    }
}
XStream xStream = new XStream(new DomDriver());
            xStream.alias("Document", B2BPayToXml.class);
            // 
            xStream.aliasField("Head", B2BPayToXml.class, "head");
            xStream.aliasField("Body", B2BPayToXml.class, "body");
            xStream.aliasField("PtcInfo", Body.class, "ptcInfo");
            xStream.aliasField("BusiInfo", Body.class, "busiInfo");
            xStream.aliasField("UserInfo", Body.class, "userInfo");
            xStream.aliasField("GoodsInfo", Body.class, "goodsInfo");
            xStream.aliasField("TranInfo", Body.class, "tranInfo");
            xStream.aliasField("ChannelInfo", Body.class, "channelInfo");
            xStream.aliasField("MemoInfo", Body.class, "memoInfo");
            B2BPayToXml bToXml = new B2BPayToXml();
            // 
            bToXml.setHead(new Head(merPtcId, tranTime, tranCode, tranDate));
            // 
            PtcInfo ptcInfo = new PtcInfo(subMerPtcId);
            // 
            BusiInfo busiInfo = new BusiInfo(merOrderNo);
            // 
            UserInfo userInfo = new UserInfo(buyerId, buyerName, sellerId, sellerName);
            // 
            GoodsInfo goodsInfo = new GoodsInfo(goodsName, goodsTxt, goodsDesc);
            // 
            TranInfo tranInfo = new TranInfo(tranModeId, tranAmt, tranCry);
            // 
            ChannelInfo channelInfo = new ChannelInfo(channelApi, channelInst);
            // 
            MemoInfo memoInfo = new MemoInfo(buyerMemo, sellerMemo, platMemo, payMemo);

            Body body = new Body();
            body.setMerTranSerialNo(merTranSerialNo);
            body.setSafeReserved(safeReserved);
            body.setPtcInfo(ptcInfo);
            body.setBusiInfo(busiInfo);
            body.setUserInfo(userInfo);
            body.setGoodsInfo(goodsInfo);
            body.setTranInfo(tranInfo);
            body.setChannelInfo(channelInfo);
            body.setMemoInfo(memoInfo);

            bToXml.setBody(body);
            String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
"; String xml = top + xStream.toXML(bToXml);

テストクラスを書きます:コードは書きません:
結果:
<?xml version="1.0" encoding="UTF-8"?> 
<Document>
  <Head>
    <MerPtcId>0000</MerPtcId>
    <TranTime>0000</TranTime>
    <TranCode>0000</TranCode>
    <TranDate>0000</TranDate>
  </Head>
  <Body>
    <MerTranSerialNo>0000</MerTranSerialNo>
    <SafeReserved>0000</SafeReserved>
    <PtcInfo>
      <SubMerPtcId>0000</SubMerPtcId>
    </PtcInfo>
    <BusiInfo>
      <MerOrderNo>0000</MerOrderNo>
    </BusiInfo>
    <UserInfo>
      <BuyerId>0000</BuyerId>
      <BuyerName>0000</BuyerName>
      <SellerId>0000</SellerId>
      <SellerName>0000</SellerName>
    </UserInfo>
    <GoodsInfo>
      <GoodsName>0000</GoodsName>
      <GoodsTxt>0000</GoodsTxt>
      <GoodsDesc>0000</GoodsDesc>
    </GoodsInfo>
    <TranInfo>
      <TranModeId>0000</TranModeId>
      <TranAmt>0000</TranAmt>
      <TranCry>0000</TranCry>
    </TranInfo>
    <ChannelInfo>
      <ChannelApi>0000</ChannelApi>
      <ChannelInst>0000</ChannelInst>
    </ChannelInfo>
    <MemoInfo>
      <BuyerMemo>0000</BuyerMemo>
      <SellerMemo>0000</SellerMemo>
      <PlatMemo>0000</PlatMemo>
      <PayMemo>0000</PayMemo>
    </MemoInfo>
  </Body>
</Document>