【发布时间】:2016-02-23 14:24:15
【问题描述】:
我一直在尝试使用 JAXB 解组以下 XML 内容。
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://wso2.org/2016/wso2as-web">
<Property Key="name">value</Property>
</Root>
在一些帖子中提到在这种情况下使用@XmlValue 注释来检索文本内容,但由于以下问题,我到目前为止失败了。
If a class has @XmlElement property, it cannot have @XmlValue property
目前我准备的代码如下:
package org.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
private Property property;
public Property getPropertyObject() {
return property;
}
public void setPropertyObject(Property property) {
this.property = property;
}
@XmlRootElement(name = "Property")
public static class Property {
@XmlAttribute(name = "Key")
private String key;
@XmlValue
private String text;
public String getKeyObject() {
return key;
}
public void setKeyObject(String key) {
this.key = key;
}
public String getValueObject() {
return text;
}
public void setValueObject(String value) {
this.text = value;
}
}
}
由于我对 JAXB 比较陌生,因此非常感谢任何有关此方面的帮助。
【问题讨论】: