xml到java pojo,带有hashmap <string,list <object =“”>&gt;

时间:2017-07-30 13:21:09

标签: java xml spring jaxb jackson

在下面给出的XML中,我无法将充值类型转换为Hashmap&gt;
twogdata,fulltalktime,special recharge是键,它的值是MycustomObject的列表 如果我尝试将此XML转换为JSON然后转换为pojo,则没有问题,但我无法直接执行此操作。我试过Jackson和JAXB

<?xml version="1.0" encoding="UTF-8"?>
    <rechargeplan>
        <Response code="200" message="success" />
        <operator name="Airtel" code="ART" />
        <circle name="Mumbai" code="ARTMU">
            <rechargetype>
                <twogdata>
                    <denomination>301</denomination>
                    <talktime>0</talktime>
                    <validity>30 days</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Access to 10 email Ids, unlimited Blackberry Messenger</remarks>
                </twogdata>
                <twogdata>
                    <denomination>175</denomination>
                    <talktime>0</talktime>
                    <validity>28 days</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>1GB 2G</remarks>
                </twogdata>

                <fulltalktime>
                    <denomination>250</denomination>
                    <talktime>250</talktime>
                    <validity>unlimited</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Talktime Rs. 250</remarks>
                </fulltalktime>
                <fulltalktime>
                    <denomination>300</denomination>
                    <talktime>300</talktime>
                    <validity>unlimited</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Talktime Rs. 300</remarks>
                </fulltalktime>

                <specialrecharge>
                    <denomination>247</denomination>
                    <talktime>0</talktime>
                    <validity>56 days</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Local Airtel mobile calls at 15p/min</remarks>
                </specialrecharge>
                <specialrecharge>
                    <denomination>655</denomination>
                    <talktime>0</talktime>
                    <validity>45 days</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>2.5 GB 3G</remarks>
                </specialrecharge>



                <topup>
                    <denomination>30</denomination>
                    <talktime>23.2</talktime>
                    <validity>unlimited</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Talktime Rs. 23.2</remarks>
                </topup>
                <topup>
                    <denomination>10</denomination>
                    <talktime>7.73</talktime>
                    <validity>unlimited</validity>
                    <servicetax></servicetax>
                    <processingfees></processingfees>
                    <validfrom></validfrom>
                    <validto></validto>
                    <remarks>Talktime Rs. 7.73</remarks>
                </topup>     


            </rechargetype>
        </circle>
    </rechargeplan>

尝试使用杰克逊

@JacksonXmlRootElement(localName = "rechargeplan")
@Getter
@Setter
public class EnRechargePlan {

    @JacksonXmlProperty(localName = "Response")
    private Response response;
    private Circle circle;
    private Operator operator;

    @Getter
    @Setter
    public class Circle {
        private String code;
        private String name;
        private HashMap<String, ArrayList<EnRechargeType>> rechargetype;

        @Getter
        @Setter
        public class EnRechargeType {

            private String denomination;
            private String talktime;
            private String validity;
            @JacksonXmlProperty(localName = "servicetax")
            private String serviceTax;
            @JacksonXmlProperty(localName = "processingfees")
            private String processingFees;
            @JacksonXmlProperty(localName = "validfrom")
            private String validFrom;
            @JacksonXmlProperty(localName = "validto")
            private String validTo;
            private String remarks;

            public EnRechargeType(String denomination, String talktime, String validity, String serviceTax, String processingFees, String validFrom, String validTo, String remarks) {
                this.denomination = denomination;
                this.talktime = talktime;
                this.validity = validity;
                this.serviceTax = serviceTax;
                this.processingFees = processingFees;
                this.validFrom = validFrom;
                this.validTo = validTo;
                this.remarks = remarks;
            }


        }


    }

    @Getter
    @Setter
    public static class Response {
        private String message;
        private String code;
    }


    @Getter
    @Setter
    private static class Operator {
        private String code;
        private String name;
    }
}

使用JAXB

@XmlRootElement(name = "rechargeplan")
public class EuroNetRechargePlan {

    //    @JacksonXmlProperty(localName = "Response")
    private Response response;
    private Circle circle;
    private Operator operator;

    @XmlElement(name = "Response")
    public Response getResponse() {
        return response;
    }

    public Circle getCircle() {
        return circle;
    }

    public void setCircle(Circle circle) {
        this.circle = circle;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Circle {

        @XmlAttribute(name = "code")
        private String code;
        @XmlAttribute(name = "name")
        private String name;
        @XmlJavaTypeAdapter(Adapter.class)
        @XmlElement(name = "rechargetype")
        private HashMap<String, ArrayList<EuroNetRechargeType>> rechargeType;

        @Data
        @XmlAccessorType(XmlAccessType.FIELD)
        public static class EuroNetRechargeType {

            private String key;
            private String denomination;
            private String talktime;
            private String validity;
            @JacksonXmlProperty(localName = "servicetax")
            private String serviceTax;
            @JacksonXmlProperty(localName = "processingfees")
            private String processingFees;
            @JacksonXmlProperty(localName = "validfrom")
            private String validFrom;
            @JacksonXmlProperty(localName = "validto")
            private String validTo;
            private String remarks;

            public EuroNetRechargeType(String denomination, String talktime, String validity, String serviceTax, String processingFees, String validFrom, String validTo, String remarks) {
                this.denomination = denomination;
                this.talktime = talktime;
                this.validity = validity;
                this.serviceTax = serviceTax;
                this.processingFees = processingFees;
                this.validFrom = validFrom;
                this.validTo = validTo;
                this.remarks = remarks;
            }


        }


    }


    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    private static class Operator {
        @XmlAttribute(name = "code")
        private String code;
        @XmlAttribute(name = "name")
        private String name;
    }

    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "")
    public static class Response {
        @XmlAttribute(name = "message")
        private String message;
        @XmlAttribute(name = "code")
        private String code;
    }
}

0 个答案:

没有答案