使用JAX-RS生成XML

时间:2017-05-28 05:43:06

标签: jax-rs

我想生成follpwing xml格式

<order_history>
    <order store_id="SSO">
            <attribute name="store_label">SSO Store</attribute>
            <attribute name="order_id">7039012</attribute>
            <attribute name="status">Shipped</attribute>
            <attribute name="total">11.76</attribute>
            <attribute name="description"/>
            <attribute name="tracking_num"/>
            <attribute name="lineItemCount">ssolineItemCount</attribute>
            <attribute name="tracking_href"/>
            <attribute name="submitted_date">11/19/2015</attribute>
            <attribute name="sort_date">1448316686906</attribute>
            <attribute name="detail_url">http://shop.scholastic.com/webapp/wcs/stores/servlet/MyAcctItemHistoryView?langId=-1&amp;storeId=10751&amp;orderId=7039011</attribute>

    </order>
<order_history>

使用以下映射类:

order.java

@XmlRootElement
public class Order {

    @XmlAttribute(name="store_id")
    private String store_id="SSO";

    @XmlAttribute
    private String store_label;
    @XmlAttribute
    private int order_id;
    @XmlAttribute
    private String status;
    @XmlAttribute
    private double total;
    @XmlAttribute
    private String description;
    @XmlAttribute
    private int tracking_num;
    @XmlAttribute
    private String lineItemCount;
    @XmlAttribute
    private String tracking_href;
    @XmlAttribute
    private String submitted_date;
    @XmlAttribute
    private int sort_date;
    @XmlAttribute
    private String detail_url;

    public String getStore_label() {
        return store_label;
    }
    public void setStore_label(String store_label) {
        this.store_label = store_label;
    }
    public int getOrder_id() {
        return order_id;
    }
    public void setOrder_id(int order_id) {
        this.order_id = order_id;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public double getTotal() {
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getTracking_num() {
        return tracking_num;
    }
    public void setTracking_num(int tracking_num) {
        this.tracking_num = tracking_num;
    }
    public String getLineItemCount() {
        return lineItemCount;
    }
    public void setLineItemCount(String lineItemCount) {
        this.lineItemCount = lineItemCount;
    }
    public String getTracking_href() {
        return tracking_href;
    }
    public void setTracking_href(String tracking_href) {
        this.tracking_href = tracking_href;
    }
    public String getSubmitted_date() {
        return submitted_date;
    }
    public void setSubmitted_date(String submitted_date) {
        this.submitted_date = submitted_date;
    }
    public int getSort_date() {
        return sort_date;
    }
    public void setSort_date(int sort_date) {
        this.sort_date = sort_date;
    }
    public String getDetail_url() {
        return detail_url;
    }
    public void setDetail_url(String detail_url) {
        this.detail_url = detail_url;
    }

OrderHistort.java

@XmlRootElement
public class OrderHistory {


    List<Order> orders = new ArrayList<Order>();


    public List<Order> getOrders() {
        return orders;
    }

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

通过这两个类,我可以生成以下xml:

<order_history>
    <order store_id="SSO">
            <store_label>SSO Store</store_label>
            <order_id>7039012</order_id>
            <status>Shipped</status>
            <total>11.76</total>
            <description>desc</description>
            <tracking_num>12234</tracking_num>
            <lineItemCount>ssolineItemCount</lineItemCount>
            <tracking_href"/>wwww.abc.com</tracking_href>
            <submitted_date>11/19/2015</submitted_date>
            <sort_date>1448316686906</sort_date>
            <detail_url>http://shop.scholastic.com/webapp/wcs/stores/servlet/MyAcctItemHistoryView?langId=-1&amp;storeId=10751&amp;orderId=7039011</detail_url>

    </order>

</order_history>

任何人都可以通过

帮助我获得所需的格式

由于

1 个答案:

答案 0 :(得分:0)

我的Order.java类

 @XmlAttribute(name="store_id")
        private String store_id="SSO";


        private String store_label;
        private int order_id;
        private String status;
        private double total;
        private String description;
        private int tracking_num;
        private String lineItemCount;
        private String tracking_href;
        private String submitted_date;
        private int sort_date;
        private String detail_url;

        public String getStore_label() {
            return store_label;
        }
        public void setStore_label(String store_label) {
            this.store_label = store_label;
        }
        public int getOrder_id() {
            return order_id;
        }
        public void setOrder_id(int order_id) {
            this.order_id = order_id;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public double getTotal() {
            return total;
        }
        public void setTotal(double total) {
            this.total = total;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public int getTracking_num() {
            return tracking_num;
        }
        public void setTracking_num(int tracking_num) {
            this.tracking_num = tracking_num;
        }
        public String getLineItemCount() {
            return lineItemCount;
        }
        public void setLineItemCount(String lineItemCount) {
            this.lineItemCount = lineItemCount;
        }
        public String getTracking_href() {
            return tracking_href;
        }
        public void setTracking_href(String tracking_href) {
            this.tracking_href = tracking_href;
        }
        public String getSubmitted_date() {
            return submitted_date;
        }
        public void setSubmitted_date(String submitted_date) {
            this.submitted_date = submitted_date;
        }
        public int getSort_date() {
            return sort_date;
        }
        public void setSort_date(int sort_date) {
            this.sort_date = sort_date;
        }
        public String getDetail_url() {
            return detail_url;
        }
        public void setDetail_url(String detail_url) {
            this.detail_url = detail_url;
        }