XStream生成XMl文件,设置别名

前端之家收集整理的这篇文章主要介绍了XStream生成XMl文件,设置别名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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","body");
            xStream.aliasField("PtcInfo",Body.class,"ptcInfo");
            xStream.aliasField("BusiInfo","busiInfo");
            xStream.aliasField("UserInfo","userInfo");
            xStream.aliasField("GoodsInfo","goodsInfo");
            xStream.aliasField("TranInfo","tranInfo");
            xStream.aliasField("ChannelInfo","channelInfo");
            xStream.aliasField("MemoInfo","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>
原文链接:https://www.f2er.com/xml/297976.html

猜你在找的XML相关文章