java Marshaller生成XML文件

使用Marshaller生成xml文件

对点进来的童鞋表示歉意,这个是留待自己回忆的,要学习童鞋可以参考下面的文章

参考文章:http://blog.csdn.net/qq496013218/article/details/74204782

Marshallerjdk1.6中自带的xml解析器,所以不需要导什么包就可以直接使用,它可以通过注解配置的方式,将java对象和xml的节点映射起来,形成java对象和xml的方便转换.

------------以下是注解,自己使用一遍有更好的理解

@XmlType,将Java类或枚举类型映射到XML模式类型

@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTYXmlAccessType.NONE

@XmlAccessorOrder,控制JAXB绑定类中属性和字段的排序。

@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()unmarshal()方法),以序列化Java类为XML

@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。

@XmlRootElement,将Java类或枚举类型映射到XML元素。

@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。

@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。

----------下面是核心代码

 

 ProjectBudgetRequest projectBudgetRequest = new ProjectBudgetRequest();
                    projectBudgetRequest.setFilename("FKTOECUBUDGET_ProjectBudgetRequest" + time + "_"  + i + ".xml");
                    projectBudgetRequest.setBudgetacc(b.getBudgetSubject());//预算科目
                    projectBudgetRequest.setActivity(b.getActivity());//活动
                    projectBudgetRequest.setChannel("00");//渠道 统一传00
                    projectBudgetRequest.setInsurance(b.getApprovalInsurance());//险种
                    projectBudgetRequest.setDescription(b.getChargeDesc());//明细行摘要
                    projectBudgetRequest.setApplyamount(b.getApplyAmount());//申请金额

                    //Request文件
                    ProjectBudgetRequestThree projectBudgetRequestThree = new ProjectBudgetRequestThree();
                    projectBudgetRequestThree.setTimeStampCreated(newDate);
                    projectBudgetRequestThree.setTimeStampExpired(newDate);
                    projectBudgetRequestThree.setId("FKTOECUBUDGET_ProjectBudgetRequest" + time + "_"  + i + ".xml");
                    ProjectBudgetRequestTwo projectBudgetRequestTwo = new ProjectBudgetRequestTwo();
                    List<ProjectBudgetRequestOne> list = new ArrayList<>();
//                    for (ProjectBudgetRequest p:projectBudgetRequestList){
                        ProjectBudgetRequestOne projectBudgetRequestOne = new ProjectBudgetRequestOne();
                        projectBudgetRequestOne.setProjectBudgetLine(projectBudgetRequest);
                        list.add(projectBudgetRequestOne);
//                    }
                    projectBudgetRequestTwo.setProjectBudgetRequest(list);
                    projectBudgetRequestThree.setCOMMAND(projectBudgetRequestTwo);
                    JAXBContext context = JAXBContext.newInstance(projectBudgetRequestThree.getClass());//使用对象生成xml
                    Marshaller marshaller = context.createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
//                marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");//放开就是GBK格式
                    try {
                        fw = new FileWriter(path.toString());
                    } catch (IOException e) {
                        logger.info("生成申请单失败",e);
                    }
                    if (null != projectBudgetRequestThree){
                        marshaller.marshal(projectBudgetRequestThree, fw);
                    }
                    fw.flush();
                    fw.close();

package com.fndsoft.schedule.model;

import javax.xml.bind.annotation.*;
import java.util.Date;

/**
 * 生成申请单xml
 * Created by zcp on 2017/8/29.
 */
// XML文件中的根标识
@XmlRootElement(name = "Message")
public class ProjectBudgetRequestThree {

    private Date timeStampExpired;//="2016-06-29T17:20:00.291+08:00"
    private String authenticationId;//="UserIdOfCPIC"
    private Date timeStampCreated;//="2016-06-29T17:20:00.288+08:00"
    private String sourceLogicalId;//="LIFE.FEE.P07MSG"
    private String sessionId;//="CPICJFO"
    private String bodyType;//="IAA-XML"
    private String txnScope;//="any"
    private String id;//="P09CHN_P07_ProjectBudgetRequest_20160629172000_178.xml"
    private String destinationLogicalId;//="CENTRAL.FINANCE.P07FINMsg"
    private String crfPublish;//="false"
    private String version;//="1.5"

    ProjectBudgetRequestTwo COMMAND;

    {
        authenticationId = "UserIdOfCPIC";
        sourceLogicalId="LIFE.FEE.P07MSG";
        sessionId="CPICJFO";
        bodyType="IAA-XML";
        txnScope="any";
        destinationLogicalId="CENTRAL.FINANCE.P07FINMsg";
        crfPublish="false";
        version="1.5";
    }

    public ProjectBudgetRequestThree() {
        super();
    }

    public ProjectBudgetRequestThree(Date timeStampExpired, String authenticationId, Date timeStampCreated, String sourceLogicalId, String sessionId, String bodyType, String txnScope, String id, String destinationLogicalId, String crfPublish, String version, ProjectBudgetRequestTwo COMMAND) {
        this.timeStampExpired = timeStampExpired;
        this.authenticationId = authenticationId;
        this.timeStampCreated = timeStampCreated;
        this.sourceLogicalId = sourceLogicalId;
        this.sessionId = sessionId;
        this.bodyType = bodyType;
        this.txnScope = txnScope;
        this.id = id;
        this.destinationLogicalId = destinationLogicalId;
        this.crfPublish = crfPublish;
        this.version = version;
        this.COMMAND = COMMAND;
    }

    @XmlElement
    public ProjectBudgetRequestTwo getCOMMAND() {
        return COMMAND;
    }

    public void setCOMMAND(ProjectBudgetRequestTwo COMMAND) {
        this.COMMAND = COMMAND;
    }

    @Override
    public String toString() {
        return "ProjectBudgetRequestThree{" +
                "COMMAND=" + COMMAND +
                '}';
    }

    @XmlAttribute
    public Date getTimeStampExpired() {
        return timeStampExpired;
    }

    public void setTimeStampExpired(Date timeStampExpired) {
        this.timeStampExpired = timeStampExpired;
    }

    @XmlAttribute
    public String getAuthenticationId() {
        return authenticationId;
    }

    public void setAuthenticationId(String authenticationId) {
        this.authenticationId = authenticationId;
    }

    @XmlAttribute
    public Date getTimeStampCreated() {
        return timeStampCreated;
    }

    public void setTimeStampCreated(Date timeStampCreated) {
        this.timeStampCreated = timeStampCreated;
    }

    @XmlAttribute
    public String getSourceLogicalId() {
        return sourceLogicalId;
    }

    public void setSourceLogicalId(String sourceLogicalId) {
        this.sourceLogicalId = sourceLogicalId;
    }

    @XmlAttribute
    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    @XmlAttribute
    public String getBodyType() {
        return bodyType;
    }

    public void setBodyType(String bodyType) {
        this.bodyType = bodyType;
    }

    @XmlAttribute
    public String getTxnScope() {
        return txnScope;
    }

    public void setTxnScope(String txnScope) {
        this.txnScope = txnScope;
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlAttribute
    public String getDestinationLogicalId() {
        return destinationLogicalId;
    }

    public void setDestinationLogicalId(String destinationLogicalId) {
        this.destinationLogicalId = destinationLogicalId;
    }

    @XmlAttribute
    public String getCrfPublish() {
        return crfPublish;
    }

    public void setCrfPublish(String crfPublish) {
        this.crfPublish = crfPublish;
    }

    @XmlAttribute
    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

}

package com.fndsoft.schedule.model;

import javax.xml.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 生成申请单xml
 * Created by zcp on 2017/8/29.
 */
@XmlAccessorType(XmlAccessType.FIELD)
// XML文件中的根标识
@XmlType(propOrder = {
        "ProjectBudgetRequest"
})
public class ProjectBudgetRequestTwo {

    private List<ProjectBudgetRequestOne> ProjectBudgetRequest;

    public ProjectBudgetRequestTwo() {
        super();
    }

    public ProjectBudgetRequestTwo(List<ProjectBudgetRequestOne> projectBudgetRequest) {
        ProjectBudgetRequest = projectBudgetRequest;
    }

    public List<ProjectBudgetRequestOne> getProjectBudgetRequest() {
        return ProjectBudgetRequest;
    }

    public void setProjectBudgetRequest(List<ProjectBudgetRequestOne> projectBudgetRequest) {
        ProjectBudgetRequest = projectBudgetRequest;
    }

    @Override
    public String toString() {
        return "ProjectBudgetRequestTwo{" +
                "ProjectBudgetRequest=" + ProjectBudgetRequest +
                '}';
    }


}

package com.fndsoft.schedule.model;

import javax.xml.bind.annotation.*;

/**
 * 生成申请单xml
 * Created by zcp on 2017/8/29.
 */
public class ProjectBudgetRequestOne {

    private String id;//C102
    private String cmdType;//request
    private String echoBack;//false
    private String cmdMode;//alwaysRespond

    ProjectBudgetRequest ProjectBudgetLine;

    {
        id = "C102";
        cmdType = "request";
        echoBack = "false";
        cmdMode = "alwaysRespond";
    }

    public ProjectBudgetRequestOne() {
        super();
    }

    public ProjectBudgetRequestOne(ProjectBudgetRequest projectBudgetLine) {
        ProjectBudgetLine = projectBudgetLine;
    }

    @XmlElement(name = "ProjectBudgetLine")
    public ProjectBudgetRequest getProjectBudgetLine() {
        return ProjectBudgetLine;
    }

    public void setProjectBudgetLine(ProjectBudgetRequest projectBudgetLine) {
        ProjectBudgetLine = projectBudgetLine;
    }

    @Override
    public String toString() {
        return "ProjectBudgetRequestOne{" +
                "ProjectBudgetLine=" + ProjectBudgetLine +
                '}';
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlAttribute
    public String getCmdType() {
        return cmdType;
    }

    public void setCmdType(String cmdType) {
        this.cmdType = cmdType;
    }

    @XmlAttribute
    public String getEchoBack() {
        return echoBack;
    }

    public void setEchoBack(String echoBack) {
        this.echoBack = echoBack;
    }

    @XmlAttribute
    public String getCmdMode() {
        return cmdMode;
    }

    public void setCmdMode(String cmdMode) {
        this.cmdMode = cmdMode;
    }

}
package com.fndsoft.schedule.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
import java.math.BigDecimal;

/**
 * 申请单生成文件Request
 * Created by zcp on 2017/8/29.
 */

@XmlAccessorType(XmlAccessType.FIELD)
// 控制JAXB 绑定类中属性和字段的排序
@XmlType(propOrder = {
        "filename",
        "budgetacc",
        "activity",
        "channel",
        "insurance",
        "description",
        "applyamount",
        "freezedamount",
        "usedamount",
        "releasedamount",
        "attribute1",
        "attribute2"
})
public class ProjectBudgetRequest {


    private String filename;//文件名(报账头文件名称,以与头文件对应)
    private String budgetacc;//预算科目
    private String activity;//活动
    private String channel;//渠道
    private String insurance;//险种
    private String description;//明细行摘要
    private BigDecimal applyamount;//申请金额
    private String freezedamount;//不传
    private String usedamount;//不传
    private String releasedamount;//不传
    private String attribute1;//不传
    private String attribute2;//不传

    public ProjectBudgetRequest() {
        super();
    }

    public ProjectBudgetRequest(String filename, String budgetacc, String activity, String channel, String insurance, String description, BigDecimal applyamount, String freezedamount, String usedamount, String releasedamount, String attribute1, String attribute2) {
        this.filename = filename;
        this.budgetacc = budgetacc;
        this.activity = activity;
        this.channel = channel;
        this.insurance = insurance;
        this.description = description;
        this.applyamount = applyamount;
        this.freezedamount = freezedamount;
        this.usedamount = usedamount;
        this.releasedamount = releasedamount;
        this.attribute1 = attribute1;
        this.attribute2 = attribute2;
    }

    public String getFilename() {
        return filename;
    }

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

    public String getBudgetacc() {
        return budgetacc;
    }

    public void setBudgetacc(String budgetacc) {
        this.budgetacc = budgetacc;
    }

    public String getActivity() {
        return activity;
    }

    public void setActivity(String activity) {
        this.activity = activity;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getInsurance() {
        return insurance;
    }

    public void setInsurance(String insurance) {
        this.insurance = insurance;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public BigDecimal getApplyamount() {
        return applyamount;
    }

    public void setApplyamount(BigDecimal applyamount) {
        this.applyamount = applyamount;
    }

    public String getFreezedamount() {
        return freezedamount;
    }

    public void setFreezedamount(String freezedamount) {
        this.freezedamount = freezedamount;
    }

    public String getUsedamount() {
        return usedamount;
    }

    public void setUsedamount(String usedamount) {
        this.usedamount = usedamount;
    }

    public String getReleasedamount() {
        return releasedamount;
    }

    public void setReleasedamount(String releasedamount) {
        this.releasedamount = releasedamount;
    }

    public String getAttribute1() {
        return attribute1;
    }

    public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
    }

    public String getAttribute2() {
        return attribute2;
    }

    public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
    }

    @Override
    public String toString() {
        return "ProjectBudgetRequest{" +
                "filename='" + filename + '\'' +
                ", budgetacc='" + budgetacc + '\'' +
                ", activity='" + activity + '\'' +
                ", channel='" + channel + '\'' +
                ", insurance='" + insurance + '\'' +
                ", description='" + description + '\'' +
                ", applyamount=" + applyamount +
                ", freezedamount='" + freezedamount + '\'' +
                ", usedamount='" + usedamount + '\'' +
                ", releasedamount='" + releasedamount + '\'' +
                ", attribute1='" + attribute1 + '\'' +
                ", attribute2='" + attribute2 + '\'' +
                '}';
    }
}

生成的xml文件内容


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message authenticationId="UserIdOfCPIC" bodyType="IAA-XML" crfPublish="false" destinationLogicalId="CENTRAL.FINANCE.P07FINMsg" id="P09CHN_P07_ProjectBudgetRequest1504147868601_1.xml" sessionId="CPICJFO" sourceLogicalId="LIFE.FEE.P07MSG" timeStampCreated="2017-08-31T10:51:08.601+08:00" timeStampExpired="2017-08-31T10:51:08.601+08:00" txnScope="any" version="1.5">
    <COMMAND>
        <projectBudgetRequest cmdMode="alwaysRespond" cmdType="request" echoBack="false" id="C102">
            <projectBudgetLine>
                <filename>P09CHN_P07_ProjectBudgetRequest1504147868601_1.xml</filename>
                <budgetacc>IC00</budgetacc>
                <activity>YSE000001</activity>
                <channel>C005</channel>
                <insurance>YSP001</insurance>
                <description>3</description>
                <applyamount>5</applyamount>
            </projectBudgetLine>
        </projectBudgetRequest>
    </COMMAND>
</Message>



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值