java bean转多层xml

业务中可能需要在xml中多层节点,可以通过新建bean类生成多节点


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

@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
public class Goods {
    private String goodsId;
    private String goodsName;
    @XmlElement(name = "goods")
    private Goods goods;

    public Goods() {
    }

    public Goods(String goodsId, String goodsName, Goods goods) {
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.goods = goods;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }
}

转换xml 类

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JaxbUtil {
	public static String converToXml(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().replace("standalone=\"yes\"", "");  
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
		
	}
	/**
	   * JavaBean转换成xml没有报文头信息
	   * @param xml
	   * @param c
	   * @return
	   */
	
	public static String converToXmlmeitou(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);
		    // 去掉生成xml的默认报文头  
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);  
			StringWriter writer=new StringWriter();
			marshaller.marshal(obj, writer);
			result=writer.toString();  
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
		
	}
	/**
	   * xml转换成JavaBean
	   * @param xml
	   * @param c
	   * @return
	   */
	  @SuppressWarnings("unchecked")
	  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;
	  }

}

运行:

public static void main(String[] args) {
 		Goods goods = new Goods();
        Goods goods1 = new Goods();
        goods1.setGoodsId("12233");
        goods1.setGoodsName("六味地黄玩");
        goods.setGoods(goods1);
        String s = converToXml(goods, "utf-8");
        System.out.println(s);
}
<?xml version="1.0" encoding="utf-8" ?>
<request>
    <goods>
        <goodsId>12233</goodsId>
        <goodsName>六味地黄玩</goodsName>
    </goods>
</request>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值