JaxbUtil-2动态拼接xml文件

package com.tarena.xmlbean.test.testbean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "bill")
public class Bill {
    @XmlElement
    private Billhead billhead;

    public Billhead getBillhead() {
        return billhead;
    }

    public void setBillhead(Billhead billhead) {
        this.billhead = billhead;
    }
}
 

package com.tarena.xmlbean.test.testbean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "billhead")
public class Billhead {
    @XmlElement
    private String creater;
    @XmlElement
    private String pk_group;
    @XmlElement
    private String pk_org;
    @XmlElement
    private String officialprintuser;

    public String getCreater() {
        return creater;
    }

    public void setCreater(String creater) {
        this.creater = creater;
    }

    public String getPk_group() {
        return pk_group;
    }

    public void setPk_group(String pk_group) {
        this.pk_group = pk_group;
    }

    public String getPk_org() {
        return pk_org;
    }

    public void setPk_org(String pk_org) {
        this.pk_org = pk_org;
    }

    public String getOfficialprintuser() {
        return officialprintuser;
    }

    public void setOfficialprintuser(String officialprintuser) {
        this.officialprintuser = officialprintuser;
    }
}
 

package com.tarena.xmlbean.test.testbean;

import javax.xml.bind.annotation.*;

import java.util.List;
/**
 * <ufinterface account="develop" billtype="F0" 
 * businessunitcode="" filename="" 
 * groupcode="001" isexchange="Y" 
 * replace="Y" roottag="" sender="ERP">
 * @author 公共
 *  第一行都是属性,其他根目录下的都是元素
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ufinterface")//设置根目录
public class Ufinterface {
    @XmlAttribute //属性
    private String account;
    @XmlAttribute //属性
    private String billtype;
    @XmlAttribute //属性
    private String businessunitcode;
    @XmlAttribute //属性
    private String filename;
    @XmlAttribute //属性
    private String groupcode;
    @XmlAttribute
    private String isexchang;
    @XmlAttribute
    private String replace;
    @XmlAttribute
    private String roottag;
    @XmlAttribute
    private String sender;
    @XmlElement(name = "bill")
    private List<Bill> billList;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getBilltype() {
        return billtype;
    }

    public void setBilltype(String billtype) {
        this.billtype = billtype;
    }

    public String getBusinessunitcode() {
        return businessunitcode;
    }

    public void setBusinessunitcode(String businessunitcode) {
        this.businessunitcode = businessunitcode;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getGroupcode() {
        return groupcode;
    }

    public void setGroupcode(String groupcode) {
        this.groupcode = groupcode;
    }

    public String getIsexchang() {
        return isexchang;
    }

    public void setIsexchang(String isexchang) {
        this.isexchang = isexchang;
    }

    public String getReplace() {
        return replace;
    }

    public void setReplace(String replace) {
        this.replace = replace;
    }

    public String getRoottag() {
        return roottag;
    }

    public void setRoottag(String roottag) {
        this.roottag = roottag;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public List<Bill> getBillList() {
        return billList;
    }

    public void setBillList(List<Bill> billList) {
        this.billList = billList;
    }
}
 

aatext.xml
<?xml version='1.0' encoding='gb2312'?>
        <ufinterface account='develop' billtype='F0' businessunitcode='' filename='' groupcode='001' isexchange='Y' replace='Y' roottag='' sender='ERP'>
        <bill>
        <billhead>
        <creater>ERP</creater>//后补的        
        <pk_group>001</pk_group>
        <pk_org>0101</pk_org>
        <officialprintuser>ERP</officialprintuser>
        <officialprintdate>"+nowDate+"</officialprintdate>
        <pk_billtype>F0</pk_billtype>
        <pk_tradetype>F0-Cxx-02</pk_tradetype>
        <confirmuser>ERP</confirmuser>
        <src_syscode>17</src_syscode>
        <syscode>0</syscode>
        <billstatus>1</billstatus>
    //    <billdate>2017-08-01 14:36:12</billdate>
        <billdate>"+nowDate+"</billdate>//起租日期odate=====================================================起租日期odate
        <billmaker>ERP</billmaker>
        <approver>ERP</approver>
        <approvedate>"+nowDate+"</approvedate>
        <rate>1</rate>
        <effectstatus>10</effectstatus>
        <pk_deptid>"+globalStartRentBean.getNcdeptno()+"</pk_deptid>
        <pk_psndoc>"+globalStartRentBean.getBcode()+"</pk_psndoc>//9项目经理
        <customer>"+globalStartRentBean.getCcodetrust()+"</customer>
        <pk_currtype>CNY</pk_currtype>
        <objtype>0</objtype>
        <scomment>"+globalStartRentBean.getPcode()+"</scomment>
        <def2>"+globalStartRentBean.getInvcode()+"</def2>//6单据号
        <def3>"+globalStartRentBean.getIndustry()+"</def3>
        <def7>"+globalStartRentBean.getPicode()+"</def7>
        <def8>"+globalStartRentBean.getPcode()+"</def8>
        <def9>"+globalStartRentBean.getLeas_type()+"</def9>
        <def4>"+globalStartRentBean.getChangesign()+"</def4>
                <bodys>
                <item>
                <customer>"+globalStartRentBean.getCcodetrust()+"</customer>
                <postunit>
                </postunit>            
                <pausetransact>N</pausetransact>
                <pk_billtype>F0</pk_billtype>
                <billclass>ys</billclass>
                <pk_tradetype>F0-Cxx-02</pk_tradetype>
                <busidate>"+Odate+"</busidate>
                <objtype>0</objtype>
                <direction>1</direction>
                <buysellflag>1</buysellflag>
                <pk_currtype>CNY</pk_currtype>
                <rate>1</rate>
                <pk_deptid>111</pk_deptid>
                <pk_psndoc>"+globalStartRentBean.getBcode()+"</pk_psndoc>//9项目经理
                <money_de>"+globalStartRentBean.getRmb()+"</money_de>
                <local_money_de>"+globalStartRentBean.getRmb()+"</local_money_de>
                <notax_de>"+globalStartRentBean.getRmb()+"</notax_de>
                <local_notax_de>"+globalStartRentBean.getRmb()+"</local_notax_de>
                <taxrate>"+(newRemark_2==null?"0.00":newRemark_2)+"</taxrate>
                <def16>"+(globalStartRentBean.getRemark_o().equals("营业税")?"1":"2")+"</def16>
                <def7>"+globalStartRentBean.getInvtype()+"</def7>
                <contractno>"+globalStartRentBean.getOrdcode()+"</contractno>
                <def18>"+(newRemark_2==null?"0.00":newRemark_2)+"</def18>
                <def19>"+(newRemark_2==null?"0.00":newRemark_2)+"</def19>
                </item>
                </bodys>
        </billhead>
        </bill>
        </ufinterface>

@XmlAccessorType(XmlAccessType.FIELD)    <ufinterface(元素) account='develop' billtype='F0' businessunitcode='' filename='' groupcode='001' isexchange='Y' replace='Y' roottag='' sender='ERP'></ufinterface>
@XmlRootElement(name = "ufinterface")//设置根目录

@XmlAttribute //属性指的节点中的一些属性

@XmlElement(name = "bill") //元素--指的xml节点(可以这样理解:元素是成对的,属性是单个的)

util

package com.tarena.xmlbean.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

public class JaxbUtil {
    public static String convertToXml(Object obj) {
        return convertToXml(obj, "UTF-8");//EN:Setting the XML encoding format   CH:设置xml中的编码格式
    }

    public static String convertToXml(Object obj, String encoding) {
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <T> T converyToJavaBean(String xml, Class<T> c) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值