XStream生成XMl文件,设置别名

树冢
2021-10-26 / 0 评论 / 8 阅读 / 正在检测是否收录...

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: 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;
    }
}
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\"?> \n";
            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>
0

评论 (0)

取消