简体   繁体   English

解组时Jaxb和Object类未从Xml获取值

[英]Jaxb and Object class do not get the value from the Xml when unmarshal

I have a problem with the following source code. 我对以下源代码有疑问。 After unmarshalling the "Simple" class, the property "content" created from the "content" tag is null. 解组“ Simple”类后,从“ content”标签创建的属性“ content”为空。 I mean the "content" property don't get the value "123456" from the xml. 我的意思是“内容”属性没有从xml中获取值“ 123456”。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Simple", propOrder = {
    "content"})
public class Simple extends Valor{

    @XmlElements({
        @XmlElement(name="content",type=String.class),
        @XmlElement(name="content",type=Integer.class),
    })
    protected Object content;

    //getters y setters
}

file.xml file.xml

<simple>
    <content>
        123456
    <content>
</simple>

Valor class 英勇阶层

@XmlSeeAlso({
Varios.class,
Simple.class,
Grilla.class
})
@XmlTransient
public abstract class Valor {
    public abstract String getValorString();
}

Any thoughts? 有什么想法吗? Thanks in advance! 提前致谢!

Adding some clarity to the issue I'm having. 为我遇到的问题增加了一些清晰度。 If I put the Simple class: 如果我将简单类放在:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Simple", propOrder = {
"content"})
public class Simple extends Valor{

    @XmlElements({
        @XmlElement(name="content",type=String.class),
        @XmlElement(name="content",type=Integer.class),
    })
    protected Object content;

    //getters y setters
}

if the xml is: 如果xml是:

<simple>
<content>
    123456
</content>
</simple>

the output is: 输出为:

123456

but if the xml is: 但是如果xml是:

<simple>
<content>
    Hi!!
</content>
</simple>

the output is: 输出为:

null

Now if the Simple class, exchange the XmlElements as in the following code: 现在,如果是Simple类,请按照以下代码交换XmlElements:

    @XmlElements({
        @XmlElement(name="content",type=Integer.class),
        @XmlElement(name="content",type=String.class)
    })
    protected Object content;

if the xml is: 如果xml是:

<simple>
<content>
    123456
</content>
</simple>

the output is: 输出为:

null

but if the xml is: 但是如果xml是:

<simple>
    <content>
        Hi!!
    </content>
</simple>

the output is: 输出为:

Hi!!

I haven't been able to reproduce what you are seeing. 我无法复制您所看到的内容。 Below is what I have tried. 下面是我尝试过的。

Java Model Java模型

Simple 简单

Below is what I implemented for your Simple class. 以下是我为您的Simple类实现的内容。

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Simple", propOrder = {
    "content"})
public class Simple extends Valor{

    @XmlElements({
        @XmlElement(name="content",type=String.class),
        @XmlElement(name="content",type=Integer.class),
    })
    protected Object content;

    public Object getContent() {
        return content;
    }

    @Override
    public String getValorString() {
        throw new UnsupportedOperationException();
    }

    public void setContent(Object content) {
        this.content = content;
    }

}

Demo Code 示范代码

input.xml input.xml中

Below is the input document with the close tag for content corrected. 以下是带有关闭标签的输入文档,其中content更正。

<simple>
    <content>
        123456
    </content>
</simple>

Demo 演示

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Simple.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource source = new StreamSource("src/forum22911170/input.xml");
        Simple simple = unmarshaller.unmarshal(source, Simple.class).getValue();

        System.out.println(simple.getContent());
    }

}

Output 产量

Below is the output from running the demo code: 以下是运行演示代码的输出:

123456

UPDATE UPDATE

The problem you are running into is that you have both options in the @XmlElements annotation mapped to the same element content . 您遇到的问题是@XmlElements批注中的两个选项都映射到相同的元素content For unmarshalling JAXB will treat the element based on the first @XmlElement with that name. 对于解组,JAXB将基于具有该名称的第一个@XmlElement对待元素。 In the case below it will always treat it as a String , it you reversed the order it would always treat it as an Integer . 在下面的情况下,它将始终将其视为String ,如果您颠倒了将其始终视为Integer的顺序。

@XmlElements({
    @XmlElement(name="content",type=String.class),
    @XmlElement(name="content",type=Integer.class),
})
protected Object content;

The proper usage is to have them mapped to different elements such as 正确的用法是将它们映射到不同的元素,例如

@XmlElements({
    @XmlElement(name="FOO",type=String.class),
    @XmlElement(name="BAR",type=Integer.class),
})
protected Object content;

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

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