无法将xml转换为java对象

时间:2018-01-29 13:37:54

标签: java xml jaxb

我使用Jaxb-api版本2.2.5将xml字符串转换为java对象。 这是我的XML示例:

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

这是我的目标:

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

}

这是我的例外 但是,即使我在gettter或setter上放置@XmlElement(name =&#34; tag:home_003&#34;),这也无法解决一些问题

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

1 个答案:

答案 0 :(得分:3)

试试这个

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

    }

和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>

更新

如果您想在示例中使用'tag'命名空间,请查看下面的代码(并在此处查看: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;
    }

}

和xml:

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

更新2: 对于您在聊天时发送给我的xml(因为我认为您仍有问题)

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

您需要创建以下类:

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

}

标签是根。然后回家:

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

}

和帮助:

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