Search code examples
javajakarta-eexml-parsingjaxbjaxb2-basics

Java object into XML conversion using JAXB - annotation issue


public class RequestXml // this pojo for RequestXML
{
    private Contact[] Contact;

    public Contact[] getContact ()
    {
        return Contact;
    }

    @XmlElement(name="Contact")
    public void setContact (Contact[] Contact)
    {
        this.Contact = Contact;
    }
}

another pojo

public class Contact  // this class is for contact
{
    private String content;
    private String role;

    public String getContent ()
    {
        return content;
    }

    @XmlElement(name="content")
    public void setContent (String content)
    {
        this.content = content;
    }   

    public String getRole ()
    {
        return role;
    }

    @XmlElement(name="role")
    public void setRole (String role)
    {
        this.role = role;
    }
}

As I am getting result like below while marshalling

<Contact role="firstUser"/>
<Contact role="secondUser"/>
<Contact role="LastUser"/>

As below is the expected output:

<Contact role="firstUser">aaaa</Contact>
<Contact role="secondUser">bbbb</Contact>
<Contact role="LastUser">cccc</Contact>

Please help me on this.


Solution

  • For marshalling the field as content, use the @XmlValue annotation. For marshalling it as attribute, use the @XmlAttribute. This is how the Contract POJO looks like, along with my test:

    @XmlRootElement
    public class RequestXml {
        private Contact[] contact;
    
        @XmlElement(name = "Contact")
        public Contact[] getContact() {
            return contact;
        }
    
        public void setContact(Contact[] Contact) {
            this.contact = Contact;
        }
    }
    
    public class Contact {
        private String content;
        private String role;
    
        public Contact(String content, String role) {
            this.content = content;
            this.role = role;
        }
    
        @XmlValue
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        @XmlAttribute
        public String getRole() {
            return role;
        }
    
        public void setRole(String role) {
            this.role = role;
        }
    }
    

    The test:

    public class JaxbTest {
    
        @Test
        public void testObjectToXml() throws JAXBException {
            RequestXml requestXml = new RequestXml();
            requestXml.setContact(new Contact[]{new Contact("aaa", "bbb")});
            JAXBContext jaxbContext = JAXBContext.newInstance(RequestXml.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(requestXml, System.out);
        }
    }
    

    This provides the following output:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <requestXml>
        <Contact role="bbb">aaa</Contact>
    </requestXml>
    

    EDIT: Also, see What is the difference between using @XmlElement before field and before getter declaration? for the difference of annotating getters/setters and fields.