简体   繁体   English

无法将xml转换为java对象

[英]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. 我正在使用Jaxb-api版本2.2.5将xml字符串转换为java对象。 Here is my XML example: 这是我的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>

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 这是我的例外,但是即使我将@XmlElement(name =“ tag:home_003”)放在gettter或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

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 和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 ) 如果您想在示例中使用'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;
    }

}

and the xml: 和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) 更新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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM