Home > Back-end >  XML to Object mapping
XML to Object mapping

Time:10-16

Having hard time to map.. Calling rest and rest return response in XML format.. Help with class mapping in Java Object (method)

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

how can i convert above xml in Java class? Thank You!

CodePudding user response:

I share solution:

If you use need dependency

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

First class.


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;
                }

            }

        }

    }

}

Second class.

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();
    }

}

And Main class.



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()));
        }
    }
    
}

result this.

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