簡體   English   中英

XML 到對象映射

[英]XML to Object mapping

很難映射..調用rest和rest返回XML格式的響應..幫助Java對象中的類映射(方法)

<aname>
   <bname>
     <c id="2">
     <cname call="yes" text="no" email="yes"/>
     <acc>
        <accinfo name="ax" addr="USA"/>
     </acc>
    </c>
   </bname>
</aname>

我如何在Java類中轉換上面的xml? 謝謝你!

我分享解決方案:

如果你使用需要依賴

<!-- Jaxb -->
<dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
</dependency>  

第一堂課。


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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "bname" })
@XmlRootElement(name = "aname")
public class Aname {

    @XmlElement(required = true)
    protected Aname.Bname bname;

    /**
     * Gets the value of the bname property.
     * 
     * @return possible object is {@link Aname.Bname }
     * 
     */
    public Aname.Bname getBname() {
        return bname;
    }

    /**
     * Sets the value of the bname property.
     * 
     * @param value allowed object is {@link Aname.Bname }
     * 
     */
    public void setBname(Aname.Bname value) {
        this.bname = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "c" })
    public static class Bname {

        @XmlElement(required = true)
        protected Aname.Bname.C c;

        /**
         * Gets the value of the c property.
         * 
         * @return possible object is {@link Aname.Bname.C }
         * 
         */
        public Aname.Bname.C getC() {
            return c;
        }

        /**
         * Sets the value of the c property.
         * 
         * @param value allowed object is {@link Aname.Bname.C }
         * 
         */
        public void setC(Aname.Bname.C value) {
            this.c = value;
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = { "cname", "acc" })
        public static class C {

            @XmlElement(required = true)
            protected Aname.Bname.C.Cname cname;
            @XmlElement(required = true)
            protected Aname.Bname.C.Acc acc;
            @XmlAttribute(name = "id", required = true)
            @XmlSchemaType(name = "unsignedByte")
            protected short id;

            /**
             * Gets the value of the cname property.
             * 
             * @return possible object is {@link Aname.Bname.C.Cname }
             * 
             */
            public Aname.Bname.C.Cname getCname() {
                return cname;
            }

            /**
             * Sets the value of the cname property.
             * 
             * @param value allowed object is {@link Aname.Bname.C.Cname }
             * 
             */
            public void setCname(Aname.Bname.C.Cname value) {
                this.cname = value;
            }

            /**
             * Gets the value of the acc property.
             * 
             * @return possible object is {@link Aname.Bname.C.Acc }
             * 
             */
            public Aname.Bname.C.Acc getAcc() {
                return acc;
            }

            /**
             * Sets the value of the acc property.
             * 
             * @param value allowed object is {@link Aname.Bname.C.Acc }
             * 
             */
            public void setAcc(Aname.Bname.C.Acc value) {
                this.acc = value;
            }

            /**
             * Gets the value of the id property.
             * 
             */
            public short getId() {
                return id;
            }

            /**
             * Sets the value of the id property.
             * 
             */
            public void setId(short value) {
                this.id = value;
            }

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = { "accinfo" })
            public static class Acc {

                @XmlElement(required = true)
                protected Aname.Bname.C.Acc.Accinfo accinfo;

                /**
                 * Gets the value of the accinfo property.
                 * 
                 * @return possible object is {@link Aname.Bname.C.Acc.Accinfo }
                 * 
                 */
                public Aname.Bname.C.Acc.Accinfo getAccinfo() {
                    return accinfo;
                }

                /**
                 * Sets the value of the accinfo property.
                 * 
                 * @param value allowed object is {@link Aname.Bname.C.Acc.Accinfo }
                 * 
                 */
                public void setAccinfo(Aname.Bname.C.Acc.Accinfo value) {
                    this.accinfo = value;
                }

                @XmlAccessorType(XmlAccessType.FIELD)
                @XmlType(name = "")
                public static class Accinfo {

                    @XmlAttribute(name = "name", required = true)
                    protected String name;
                    @XmlAttribute(name = "addr", required = true)
                    protected String addr;

                    /**
                     * Gets the value of the name property.
                     * 
                     * @return possible object is {@link String }
                     * 
                     */
                    public String getName() {
                        return name;
                    }

                    /**
                     * Sets the value of the name property.
                     * 
                     * @param value allowed object is {@link String }
                     * 
                     */
                    public void setName(String value) {
                        this.name = value;
                    }

                    /**
                     * Gets the value of the addr property.
                     * 
                     * @return possible object is {@link String }
                     * 
                     */
                    public String getAddr() {
                        return addr;
                    }

                    /**
                     * Sets the value of the addr property.
                     * 
                     * @param value allowed object is {@link String }
                     * 
                     */
                    public void setAddr(String value) {
                        this.addr = value;
                    }

                }

            }

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "")
            public static class Cname {

                @XmlAttribute(name = "call", required = true)
                protected String call;
                @XmlAttribute(name = "text", required = true)
                protected String text;
                @XmlAttribute(name = "email", required = true)
                protected String email;

                /**
                 * Gets the value of the call property.
                 * 
                 * @return possible object is {@link String }
                 * 
                 */
                public String getCall() {
                    return call;
                }

                /**
                 * Sets the value of the call property.
                 * 
                 * @param value allowed object is {@link String }
                 * 
                 */
                public void setCall(String value) {
                    this.call = value;
                }

                /**
                 * Gets the value of the text property.
                 * 
                 * @return possible object is {@link String }
                 * 
                 */
                public String getText() {
                    return text;
                }

                /**
                 * Sets the value of the text property.
                 * 
                 * @param value allowed object is {@link String }
                 * 
                 */
                public void setText(String value) {
                    this.text = value;
                }

                /**
                 * Gets the value of the email property.
                 * 
                 * @return possible object is {@link String }
                 * 
                 */
                public String getEmail() {
                    return email;
                }

                /**
                 * Sets the value of the email property.
                 * 
                 * @param value allowed object is {@link String }
                 * 
                 */
                public void setEmail(String value) {
                    this.email = value;
                }

            }

        }

    }

}

第二課。

import javax.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public class ObjectFactory {

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema
     * derived classes for package: generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Aname }
     * 
     */
    public Aname createAname() {
        return new Aname();
    }

    /**
     * Create an instance of {@link Aname.Bname }
     * 
     */
    public Aname.Bname createAnameBname() {
        return new Aname.Bname();
    }

    /**
     * Create an instance of {@link Aname.Bname.C }
     * 
     */
    public Aname.Bname.C createAnameBnameC() {
        return new Aname.Bname.C();
    }

    /**
     * Create an instance of {@link Aname.Bname.C.Acc }
     * 
     */
    public Aname.Bname.C.Acc createAnameBnameCAcc() {
        return new Aname.Bname.C.Acc();
    }

    /**
     * Create an instance of {@link Aname.Bname.C.Cname }
     * 
     */
    public Aname.Bname.C.Cname createAnameBnameCCname() {
        return new Aname.Bname.C.Cname();
    }

    /**
     * Create an instance of {@link Aname.Bname.C.Acc.Accinfo }
     * 
     */
    public Aname.Bname.C.Acc.Accinfo createAnameBnameCAccAccinfo() {
        return new Aname.Bname.C.Acc.Accinfo();
    }

}

和主課。



import java.io.StringReader;
import java.text.ParseException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

    static ObjectMapper mapper = new ObjectMapper();

    public static void main(String... strings) throws ParseException {

        String xml = "<aname> <bname> <c id=\"2\"> <cname call=\"yes\" text=\"no\" email=\"yes\"/> <acc> <accinfo name=\"ax\" addr=\"USA\"/> </acc> </c> </bname> </aname>";
        
        Aname clazz = convertXMLToObject(Aname.class, xml);
        
        try {
            System.out.println(mapper.writeValueAsString(clazz));
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
    @SuppressWarnings("unchecked")
    public static <T> T convertXMLToObject(Class<T> clazz, String xml) 
    {
        try {

            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller um = context.createUnmarshaller();
            return (T) um.unmarshal(new StringReader(xml));
        } 
        catch (JAXBException je){
            throw new RuntimeException(String.format("Exception while Unmarshaller: %s", je.getMessage()));
        }
    }
    
}

結果這個。

{"bname":{"c":{"cname":{"call":"yes","text":"no","email":"yes"},"acc":{"accinfo":{"name":"ax","addr":"USA"}},"id":2}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM