Java 针对具有属性和文本内容的复杂类型的JAXB解组
我一直在尝试使用JAXB解压以下XML内容Java 针对具有属性和文本内容的复杂类型的JAXB解组,java,xml,jaxb,Java,Xml,Jaxb,我一直在尝试使用JAXB解压以下XML内容 <?xml version="1.0" encoding="utf-8"?> <Root xmlns="http://wso2.org/2016/wso2as-web"> <Property Key="name">value</Property> </Root> 到目前为止,我编写的代码如下: package org.test; import javax.xml.bind.ann
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://wso2.org/2016/wso2as-web">
<Property Key="name">value</Property>
</Root>
到目前为止,我编写的代码如下:
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比较陌生。您必须使用
@xmlacessortype(xmlacesstype.FIELD)
注释属性
否则,其getXxx()
方法将被视为元素,因为getter的名称与字段的名称不匹配。必须使用@xmlacessortype(xmlacesstype.FIELD)
注释属性
否则,它的getXxx()
方法将被视为元素,因为getter的名称与字段的名称不匹配。要添加到Benjamin的答案中,您的异常是由于内部类属性
没有注释造成的
@xmlacessortype(xmlacesstype.NONE)
或@xmlacessortype(xmlacesstype.FIELD)
要添加到Benjamin的答案中,您的异常是由于内部类属性
没有注释造成的
@xmlacessortype(xmlacesstype.NONE)
或@xmlacessortype(xmlacesstype.FIELD)
我找到了上述案例的答案。以上两个答案确实解决了弹出的异常问题(在文章中提到),但没有加载任何与加载相关的、无法正常工作的内容
以下是固定代码:
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.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
@XmlElement(name = "Property")
private Property property;
public Property getPropertyObject() {
return property;
}
public void setPropertyObject(Property property) {
this.property = property;
}
@XmlAccessorType(XmlAccessType.FIELD)
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;
}
}
}
我从嵌套的属性类中删除了@XmlRootElement(name=“Property”)注释,并将@XmlElement(name=“Property”)添加到Root.java的属性实例变量中。我找到了上述情况的答案。以上两个答案确实解决了弹出的异常问题(在文章中提到),但没有加载任何与加载相关的、无法正常工作的内容
以下是固定代码:
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.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
@XmlElement(name = "Property")
private Property property;
public Property getPropertyObject() {
return property;
}
public void setPropertyObject(Property property) {
this.property = property;
}
@XmlAccessorType(XmlAccessType.FIELD)
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;
}
}
}
我从嵌套的属性类中删除了@XmlRootElement(name=“Property”)注释,并将@XmlElement(name=“Property”)添加到Root.java的属性实例变量中。@Make-sense。这是有道理的。编辑。