Feres.o
Feres.o

Reputation: 283

can't convert xml to java object

I'm using Jaxb-api version 2.2.5 to convert an xml String to a java Object. Here is my XML example:

    <tag:TAG xmlns:tag="xxxxxxxx/tag" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xxxxxxxx/tag RCARRVAL.xsd">
        <tag:home>
            <tag:home_001>01</tag:home_001>
            <tag:home_002>0032</tag:home_002>
            <tag:home_003>1977</tag:home_003>
            <tag:home_004>4</tag:home_004>
            <tag:home_005>4</tag:home_005>
            <tag:home_010>2017-12-31</tag:home_010>
            <tag:home_999>RG01</tag:home_999>
        </tag:home>
</tag:TAG>

here is my object:

       @XmlRootElement(name="tag:home")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer  {


    protected Date dateDebut;

    @XmlElement(name="tag:home_003")
    public Date getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

}

and here is my exceptions But this is not working i mfacing some issues / even when i put @XmlElement(name="tag:home_003") on gettter or on setter

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
La classe comporte deux propriétés du même nom ("dateDebut")
    this problem is related to the following location:
        at public java.util.Date fr.models.Customer.getDateDebut()
        at fr.models.Customer
    this problem is related to the following location:
        at protected java.util.Date fr.models.Customer.dateDebut
        at fr.models.Customer

Upvotes: 2

Views: 991

Answers (1)

martidis
martidis

Reputation: 2975

Try this

    @XmlRootElement(name="home")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer  {

        @XmlElement(name="home_003")
        protected Date dateDebut;


        public Date getDateDebut() {
            return dateDebut;
        }

        public void setDateDebut(Date dateDebut) {
            this.dateDebut = dateDebut;
        }

    }

and xml

    <home>
        <!--<home_001>01</home_001>-->
        <!--<home_002>0032</home_002>-->
        <home_003>1977</home_003>
        <!--<home_004>4</home_004>-->
        <!--<home_005>4</home_005>-->
        <!--<home_010>2017-12-31</home_010>-->
        <!--<home_999>RG01</home_999>-->
    </home>

Update:

In case you want to use the 'tag' namespace as in your example, check the code below (and also take a look here: https://en.wikipedia.org/wiki/XML_namespace)

@XmlRootElement(name="home", namespace = "http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer  {

    @XmlElement(name="home_003", namespace = "http://www.example.com")
    protected Date dateDebut;

    public Date getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

}

and the xml:

<tag:home xmlns:tag="http://www.example.com">
    <tag:home_003>1977</tag:home_003>
</tag:home>

Update 2: For the xml you sent me on chat (since I think you still have problems)

<tag:TAG xmlns:tag="xxxxxxxx/tag" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xxxxxxxx/tag RCARRVAL.xsd">
    <tag:home>
        <tag:home_001>01</tag:home_001>
        <tag:home_002>0032</tag:home_002>
        <tag:home_003>1977</tag:home_003>
        <tag:home_004>4</tag:home_004>
        <tag:home_005>4</tag:home_005>
        <tag:home_010>2017-12-31</tag:home_010>
        <tag:home_999>RG01</tag:home_999>
    </tag:home>
    <tag:help>
    <tag:help_010>2017-12-31</tag:help_010>
    <tag:help_999>RG01</tag:help_999>
    </tag:help>
</tag:TAG>

You need to create the following classes:

@XmlRootElement(name="TAG", namespace = "xxxxxxxx/tag")
@XmlAccessorType(XmlAccessType.FIELD)
public class Tag {

    @XmlElement(name = "home", namespace = "xxxxxxxx/tag")
    private Home home;
    @XmlElement(name = "help", namespace = "xxxxxxxx/tag")
    private Help help;

}

Tag is the root. Then Home:

@XmlAccessorType(XmlAccessType.FIELD)
public class Home {

    @XmlElement(name="home_001", namespace = "xxxxxxxx/tag")
    private String home_001;
    @XmlElement(name="home_002", namespace = "xxxxxxxx/tag")
    private String home_002;
    @XmlElement(name="home_003", namespace = "xxxxxxxx/tag")
    private Date home_003;
    @XmlElement(name="home_004", namespace = "xxxxxxxx/tag")
    private int home_004;
    @XmlElement(name="home_005", namespace = "xxxxxxxx/tag")
    private int home_005;
    @XmlElement(name="home_010", namespace = "xxxxxxxx/tag")
    private Date home_010;
    @XmlElement(name="home_999", namespace = "xxxxxxxx/tag")
    private String home_999;

}

and Help:

@XmlAccessorType(XmlAccessType.FIELD)
public class Help {
    @XmlElement(name="help_010", namespace = "xxxxxxxx/tag")
    private Date help_010;
    @XmlElement(name="help_999", namespace = "xxxxxxxx/tag")
    private String help_999;
}

Upvotes: 3

Related Questions