I have a problem with the following source code. After unmarshalling the "Simple" class, the property "content" created from the "content" tag is null. I mean the "content" property don't get the value "123456" from the xml.
@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
<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:
<simple>
<content>
123456
</content>
</simple>
the output is:
123456
but if the xml is:
<simple>
<content>
Hi!!
</content>
</simple>
the output is:
null
Now if the Simple class, exchange the XmlElements as in the following code:
@XmlElements({
@XmlElement(name="content",type=Integer.class),
@XmlElement(name="content",type=String.class)
})
protected Object content;
if the xml is:
<simple>
<content>
123456
</content>
</simple>
the output is:
null
but if the xml is:
<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.
Simple
Below is what I implemented for your Simple
class.
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;
}
}
input.xml
Below is the input document with the close tag for content
corrected.
<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
The problem you are running into is that you have both options in the @XmlElements
annotation mapped to the same element content
. For unmarshalling JAXB will treat the element based on the first @XmlElement
with that name. 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
.
@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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.