xml转换方式记载

一、背景

最近接接口,然后了,返回的接口有两种格式,一种是xml的,一种是json格式的,所以我首选是接收json格式的,但是json格式的数据有一个弊端,那就是字段名称全是简写的。但是xml的名称都是见名知意的,两种方式都尝试了下,最后还是选择xml的格式。

二、转换方式

xml格式的直接转对象,也有好多选择,我这边使用了两种方式,其一是XStream,另外一种是jaxb;现在分别数说说了!!!
(1)Xtream


 
  success  
  成功  
   
     
      是我的  
      2016-12-26  
      2016-12-27  
      是我的  
      深圳南澳520度假主题客栈  
      HT16122601101  
      1  
      fasfsa  
      2016-12-26 16:19:35.0  
      
     
      就是我的  
      2016-09-06  
      2016-09-07  
      就是我的  
      深圳南澳520度假主题客栈  
      HT16090600362  
      1  
      dfas  
      2016-09-06 14:06:15.0  
      
    

需要转换的xml字符串
转换代码,result就是上面的字符串

String result = HttpClient.order("getOrderList", orderParam);
XStream xstream = new XStream(); 
xstream.alias("result", ResultBean.class); 
xstream.alias("order", Order.class); 
orderInfo = (ResultBean) xstream.fromXML(result);

给出ResultBean对象和Order对象数据

package com.mouse.moon.xml;
import java.util.List;
public class ResultBean {
    private String errorCode;
    private String errorMsg;
    private  List orders;
    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public List getOrders() {
        return orders;
    }

    public void setOrders(List orders) {
        this.orders = orders;
    }
}   

package com.mouse.moon.xml;
public class Order {
    private String booker;
    private String checkInDate;
    private String checkOutDate;
    private String guest;
    private String hotelName;
    private String orderNo;
    private String orderStatus;
    private String orderStatusName;
    private String orderTime;
    public String getBooker() {
        return booker;
    }
    public void setBooker(String booker) {
        this.booker = booker;
    }
    public String getCheckInDate() {
        return checkInDate;
    }
    public void setCheckInDate(String checkInDate) {
        this.checkInDate = checkInDate;
    }
    public String getCheckOutDate() {
        return checkOutDate;
    }
    public void setCheckOutDate(String checkOutDate) {
        this.checkOutDate = checkOutDate;
    }
    public String getGuest() {
        return guest;
    }
    public void setGuest(String guest) {
        this.guest = guest;
    }
    public String getHotelName() {
        return hotelName;
    }
    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }
    public String getOrderNo() {
        return orderNo;
    }
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }
    public String getOrderStatus() {
        return orderStatus;
    }
    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }
    public String getOrderStatusName() {
        return orderStatusName;
    }
    public void setOrderStatusName(String orderStatusName) {
        this.orderStatusName = orderStatusName;
    }
    public String getOrderTime() {
        return orderTime;
    }
    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }
}

所以如上述转换代码所示,需要将你的对象与xml的节点数据对应上

xstream.alias("result", ResultBean.class); 

另外说一点,比如有的数据节点上还有数据


 
 success  
 成功  
  
    
      我得
     
   

比如在order节点上还有一个预订人的id,那么在order类就需要做修改
就需要在Order如下

@XStreamConverter(value=ToAttributedValueConverter.class, strings={"booker"}) 
public class Order {
    private String booker;
    @XStreamAsAttribute
    private String booerId;
    public String getBooker() {
        return booker;
    }
    public void setBooker(String booker) {
        this.booker = booker;
    }
    public String getBooerId() {
        return booerId;
    }
    public void setBooerId(String booerId) {
        this.booerId = booerId;
    }
}

(2)JAXB

String result = HttpClient.order("getOrderList", orderParam);
StringReader read = new StringReader(result);
InputSource source = new InputSource(read);
JAXBContext jaxbContext = JAXBContext.newInstance(ResultBean .class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ResultBean resultBean= (ResultBean ) jaxbUnmarshaller.unmarshal(source);

类中需要做注解配置

package com.mouse.moon.xml;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="result")
@XmlType(propOrder = { "errorCode","errorMsg", "orders"})
public class ResultBean {
    private String errorCode;
    private String errorMsg;
    private  List orders;
    public String getErrorCode() {
        return errorCode;
    }
    @XmlElement
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
    public String getErrorMsg() {
        return errorMsg;
    }
    @XmlElement
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
    public List getOrders() {
        return orders;
    }
    @XmlElement
    public void setOrders(List orders) {
        this.orders = orders;
    }
}       
package com.mouse.moon.xml;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Order {
    private String booker;
    private String checkInDate;
    private String checkOutDate;
    private String guest;
    private String hotelName;
    private String orderNo;
    private String orderStatus;
    private String orderStatusName;
    private String orderTime;
    public String getBooker() {
        return booker;
    }
    public void setBooker(String booker) {
        this.booker = booker;
    }
    public String getCheckInDate() {
        return checkInDate;
    }
    public void setCheckInDate(String checkInDate) {
        this.checkInDate = checkInDate;
    }
    public String getCheckOutDate() {
        return checkOutDate;
    }
    public void setCheckOutDate(String checkOutDate) {
        this.checkOutDate = checkOutDate;
    }
    public String getGuest() {
        return guest;
    }
    public void setGuest(String guest) {
        this.guest = guest;
    }
    public String getHotelName() {
        return hotelName;
    }
    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }
    public String getOrderNo() {
        return orderNo;
    }
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }
    public String getOrderStatus() {
        return orderStatus;
    }
    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }
    public String getOrderStatusName() {
        return orderStatusName;
    }
    public void setOrderStatusName(String orderStatusName) {
        this.orderStatusName = orderStatusName;
    }
    public String getOrderTime() {
        return orderTime;
    }
    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }
}

这个其实在网上也是有资料的https://www.javacodegeeks.com/2013/02/jaxb-tutorial-getting-started.html
注意在一级中的每个set属性上都要加上 @XmlElement,且不要再get方法上加,否则转换报错
使用jaxb将对象转换为xml的方式如下

      StringWriter writer = new StringWriter();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.marshal(bean, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return writer.getBuffer().toString();

浩语

                                          __                                                        
                            __  _  ____ __|  |__ _____    ___
                            \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                             \     /|  |  /   Y  \/ __ \(  <_> )
                              \/\_/ |____/|___|  (____  /\____/ 
                                                    \/     \/          
          走在自己的路上,遇到要遇到的人,经历要经历的事,这才是我们需要面对的。
                                                       

你可能感兴趣的:(xml转换方式记载)