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
8
  • Could you share full Customer class? Commented Jan 29, 2018 at 13:41
  • it is the same class, just with one field to test transofrmation from xml to java object, like many Tutorials Commented Jan 29, 2018 at 13:45
  • I don't think so, look in the exceptions's stack, r.models.Customer.dateDebut or in the sared class we can see only homeDate. Commented Jan 29, 2018 at 13:51
  • ah yes it's a typo mistake i changed the class field :) sorry Commented Jan 29, 2018 at 14:04
  • Could you just copy/paste your class? Exception says La classe comporte deux propriétés du même nom ("homeDate")and there is no homeDate attribute in customer's class. Commented Jan 29, 2018 at 14:07

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
it works but , i'm referring to my exxample why this is not working with my xml file <tag:home> and tag:home_003, ? is there a way to consider the same xml balise
Will update in response to include namespaces in the example
i updated my example of xml file , so the roor elment will be be tag ?
the namespace you declare in your xml for tag (in your example is xxxxxxxx/tag) should also be declared in @XmlElement 's namespace
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.