通过JAXB,将javaBean转xml

最终效果:

代码内容:

1、请求的总体结构model(RequestModel)

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


@XmlRootElement(name = "Request")  
@XmlAccessorType(XmlAccessType.PROPERTY)  
@XmlType(name = "", propOrder = { "head", "body" })  
public class RequestModel<T extends Body<?>>{

    protected Head head;  
   
    protected T body;
    
    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @XmlElement(name = "Head", required = true)  
    public Head getHead() {  
        return head;  
    }  
 
    public void setHead(Head value) {  
        this.head = value;  
    }  
 
}

2、内容model(Body)

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


@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
public class Body<T> {
    
    private T  body;
    
    private String head;

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }
    
    @XmlMixed
    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }
}

3、xml中头结构Head

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


@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
public class Head {

    public String summary;

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}


4、简单的测试bean(User、Customer)

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;  
import javax.xml.bind.annotation.XmlAccessorType;  
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
    
    private String name;
    private Integer age;
    
    
    @XmlElementWrapper(name="customerList")
    @XmlElement(name="customer")
    private List<Customer> customer;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public List<Customer> getCustomer() {
        return customer;
    }
    public void setCustomer(List<Customer> customer) {
        this.customer = customer;
    }
}

public class Customer {

    private String commodity;

    public String getCommodity() {
        return commodity;
    }

    public void setCommodity(String commodity) {
        this.commodity = commodity;
    }
}

5、解析工具类(XmlUtil)

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

public class XmlUtil {

    
    public static String toXmlString(Object objBean) throws Exception{
         Body<Object> com = new Body<Object>();
         com.setBody(objBean);
         com.setHead("test");
         RequestModel<Body<?>> re = new RequestModel<Body<?>>();
         re.setBody(com);
         Head h = new Head();
         h.setSummary("test");
         re.setHead(h);
         JAXBContext context = JAXBContext.newInstance(RequestModel.class,Head.class,objBean.getClass());
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
         StringWriter writer = new StringWriter();
         marshaller.marshal(re, writer);
         return writer.toString();
    }
}


6、测试main

import java.util.ArrayList;
import java.util.List;


public class Test {

    public static void main(String[] args) {
         //创建user对象与customer对象并赋值
        User user = new User();
        Customer customer1 = new Customer();
        Customer customer2 = new Customer();
        customer1.setCommodity("商品1");
        customer2.setCommodity("商品2");
        List<Customer> list = new ArrayList<Customer>();
        list.add(customer1);
        list.add(customer2);
        user.setName("beyondLi");
        user.setAge(23);
        user.setCustomer(list);
        
        
        try {
            System.out.println(XmlUtil.toXmlString(user));;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值