2

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
Feres.o
  • 283
  • 1
  • 4
  • 16

1 Answers1

3

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;
}
martidis
  • 2,897
  • 1
  • 11
  • 13
  • XmlAccessType.FIELD checks the fields and XmlAccessType.PROPERTY checks the getter/setter. I simplified your example (didnt create namespace for tag, etc) to run it faster, so if it does not work for you, please let me know – martidis Jan 29 '18 at 14:47
  • it works but , i'm referring to my exxample why this is not working with my xml file and tag:home_003, ? is there a way to consider the same xml balise – Feres.o Jan 29 '18 at 15:02
  • Will update in response to include namespaces in the example – martidis Jan 29 '18 at 15:25
  • i updated my example of xml file , so the roor elment will be be tag ? – Feres.o Jan 29 '18 at 15:43
  • the namespace you declare in your xml for tag (in your example is xxxxxxxx/tag) should also be declared in @XmlElement 's namespace – martidis Jan 29 '18 at 15:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164105/discussion-between-feres-o-and-mart). – Feres.o Jan 29 '18 at 15:48