Java JAXB 实体对象转xml

TestJsonController

package com.xxx.api.controller.business;

import com.xxx.business.domain.Body;
import com.xxx.business.domain.Business;
import com.xxx.business.domain.Fyxm;
import com.xxx.business.domain.Group;
import com.xxx.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

/**
 * @author jincheng
 * @date 2021/12/21
 */
@Api(tags = "api接口")
@RestController
@RequestMapping("/api")
@Slf4j
public class TestJsonController {
    @ApiOperation(value = "测试")
    @PostMapping("/test")
    public AjaxResult test() throws JAXBException {
        Business business = new Business();
        Body body = new Body();
        body.setYylxdm("1");
        Fyxm fyxm = new Fyxm();
        fyxm.setCount("1");
        Group group = new Group();
        group.setXh("1");
        group.setDj("dj");
        fyxm.setGroup(group);
        body.setFyxm(fyxm);
        business.setBody(body);
        business.setComment("ceshi");
        business.setId(30010);
        String result = beanToXml(business, Business.class);
        System.out.println(result);
        return AjaxResult.success();
    }

    private String beanToXml(Object obj, Class<?> load) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(load);
        Marshaller marshaller = context.createMarshaller();
        // 换行符  true换行 false不换行
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //设置encoding 
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");
        // 去掉xmlheader
//        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    }
}

实体类

Business

package com.xxx.business.domain;

import lombok.Data;

import javax.xml.bind.annotation.*;

/**
 * @author jincheng
 * @date 2021/12/21
 */
@Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Business {
    @XmlAttribute
    //节点属性
    private int id;

    @XmlAttribute
    //节点属性
    private String comment;

    private Body body;
}

Body

package com.xxx.business.domain;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

/**
 * @author jincheng
 * @date 2021/12/21
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
    @XmlAttribute
    private String yylxdm;
    private String kplx;
    private String qdbz;
    private String zsfs;
    private Fyxm fyxm;
    
}

Fyxm

package com.xxx.business.domain;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

/**
 * @author jincheng
 * @date 2021/12/21
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Fyxm {
    @XmlAttribute
    private String count;
    private Group group;
}

Group

package com.xxx.business.domain;

import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

/**
 * @author jincheng
 * @date 2021/12/21
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Group {
        @XmlAttribute
        private String xh;
        private String fphxz;
        private String spmc;
        private String dj;
  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值