将XML解析为具有XML中字段类型的Java对象

时间:2018-11-28 19:30:12

标签: java xml xml-parsing

  

我有这个xml,我需要将其解析为java对象。有人可以帮忙吗?这种扫描阵列可能会很长。当我使用JAXB.unmarshaller进行解析时,它只能解析pk和模型吗?

<?xml version="1.0" encoding="utf-8"?>
<ecomexpress-objects version="1.0">
    <object pk="1" model="awb">
        <field type="BigIntegerField" name="awb_number">285213888</field>
        <field type="CharField" name="orderid">5319460</field>
        <field type="FloatField" name="actual_weight">0.6</field>
        <field type="CharField" name="origin">GURGAON - GGP</field>
        <field type="CharField" name="destination">GURGAON - GGD</field>
        <field type="CharField" name="current_location_name">GURGAON - GGD</field>
        <field type="CharField" name="current_location_code">GGD</field>
        <field type="CharField" name="consignee">SEEMS</field>
        <field type="CharField" name="pickupdate">%d-%b-%Y</field>
        <field type="CharField" name="status">Delivered / Closed</field>
        <field type="CharField" name="tracking_status">Delivered</field>
        <field type="CharField" name="reason_code">999 - Delivered</field>
        <field type="CharField" name="reason_code_description">Delivered</field>
        <field type="CharField" name="reason_code_number">999</field>
        <field type="CharField" name="receiver">Security:jony: Android</field>
        <field type="CharField" name="lat">28.4849986</field>
        <field type="CharField" name="long">77.0637018</field>
        <field type="CharField" name="expected_date" >%d-%b-%Y</field>
        <field type="CharField" name="last_update_date" >%d-%b-%Y</field>
        <field type="CharField" name="delivery_date" >%Y-%m-%d %H:%i:%s</field>
        <field type="CharField" name="ref_awb" >None</field>
        <field type="CharField" name="rts_shipment" >0</field>
        <field type="CharField" name="system_delivery_update" >%Y-%m-%d %H:%i:%s</field>
        <field type="CharField" name="rts_system_delivery_status" />
        <field type="CharField" name="rts_reason_code_number"/>
        <field type="CharField" name="rts_last_update"/>
        <field type="CharField" name="pincode" >122015</field>
        <field type="CharField" name="city" >GURUGRAM</field>
        <field type="CharField" name="state" >Haryana</field>
        <field type="CharField" name="delivery_pod_image" />
        <field type="CharField" name="rev_pickup_signature" />
        <field name="scans">
            <object pk="1" model="scan_stages">
                <field type="DateTimeField" name="updated_on">%d %b, %Y, %H:%i 
                </field>
                <field type="CharField" name="status">Shipment 
            delivered</field>
                <field type="CharField" name="reason_code">999 - 
             Delivered</field>
                <field type="CharField" name="reason_code_number">999</field>
                <field type="CharField" name="scan_status">HOLD</field>
                <field type="CharField" name="location">GGD</field>
                <field type="CharField" name="location_city">GURUGRAM</field>
                <field type="CharField" name="location_type">Service 
           Center</field>
                <field type="CharField" name="city_name">GURUGRAM</field>
                <field type="CharField" name="Employee">Vijay Kumar - 
            12573</field>
            </object>
        </field>
    </object>
</ecomexpress-objects>
  

有人可以告诉我解析此xml的最有效方法吗?我正在将其解析到下面的这个对象,但是不知何故它只是解析pk和模型。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "object"
    })
@XmlRootElement(name = "ecomexpress-objects")
public class EcomExpressObjects {

    @XmlElement(name = "object", required = true)
    protected EcomExpressObjects.Object object;

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Object {

        @XmlElement(name = "awb_number", required = true)
        protected BigInteger awb_number;

        @XmlElement(name = "orderid")
        protected String orderid;

        @XmlElement(name = "actual_weight")
        protected Float actualWeight;

        @XmlElement(name = "origin", required = true)
        protected String origin;

        @XmlElement(name = "destination")
        protected String destination;

        @XmlElement(name = "current_location_name")
        protected String currentLocationName;

        @XmlElement(name = "current_location_code")
        protected String currentLocationCode;

        @XmlElement(name = "customer")
        protected String customer;

        @XmlElement(name = "consignee")
        protected String consignee;

        @XmlElement(name = "pickupdate")
        protected String pickupdate;

        @XmlElement(name = "status", required = true)
        protected String status;

        @XmlElement(name = "tracking_status")
        protected String trackingStatus;

        @XmlElement(name = "reason_code")
        protected String reasonCode;

        @XmlElement(name = "reason_code_description")
        protected String reasonCodeDescription;

        @XmlElement(name = "reason_code_number")
        protected String reasonCodeNumber;

        @XmlElement(name = "receiver")
        protected String receiver;

        @XmlElement(name = "expected_date")
        protected String expectedDate;

        @XmlElement(name = "last_update_date")
        protected String lastUpdateDate;

        @XmlElement(name = "delivery_date")
        protected String deliveryDate;

        @XmlElement(name = "pincode")
        protected String pincode;

        @XmlElement(name = "city")
        protected String city;

        @XmlElement(name = "state")
        protected String state;

        @XmlElement(name = "scans")
        protected EcomExpressObjects.Object.Scans scans;

        @XmlAttribute
        protected String pk;

        @XmlAttribute
        protected String model;


        @XmlAccessorType(XmlAccessType.FIELD)
        public static class Scans {

            @XmlElement(name = "scan_stages", required = true)
            protected List<EcomExpressObjects.Object.Scans.ScanStages> scanStages;

            @XmlAccessorType(XmlAccessType.FIELD)
            public static class ScanStages {
                @XmlElement(name = "updated_on")
                protected Date updatedOn;

                @XmlElement
                protected String status;

                @XmlElement(name = "reason_code")
                protected String reasonCode;

                @XmlElement(name = "reason_code_number")
                protected String reasonCodeNumber;

                @XmlElement(name = "scan_status")
                protected String scanStatus;

                @XmlElement
                protected String location;

                @XmlElement(name = "location_city")
                protected String locationCity;

                @XmlElement(name = "location_type")
                protected String locationType;

                @XmlElement(name = "city_name")
                protected String cityName;

            }

        }

    }
}

0 个答案:

没有答案