我正在使用Java应用程序来读取第三方应用程序生成的XML文件。我能够毫无问题地阅读XML属性,但是当我尝试映射元素的值时,它返回一个null对象。我相信我错过了一些简单的东西,但是我们无法弄明白。
实体:
import java.lang.reflect.Field;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Entity")
public class Entity {
String externalId;
List<Attribute> attributes;
@XmlAttribute(name="ExternalId")
public String getExternalId() {
return externalId;
}
public void setExternalId(String externalId) {
this.externalId = externalId;
}
@XmlElementWrapper(name="Attributes")
@XmlElement(name="Attribute")
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
Field[] fields = this.getClass().getDeclaredFields();
String res = "";
try {
for (Field field : fields) {
res += field.getName() + " : " + field.get(this) + "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
属性:
import java.lang.reflect.Field;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
public class Attribute {
String attributeName;
String attributeLongName;
List<Value> values;
@XmlAttribute(name="Name")
public String getAttributeName() {
return attributeName;
}
public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}
@XmlAttribute(name="LongName")
public String getAttributeLongName() {
return attributeLongName;
}
public void setAttributeLongName(String attributeLongName) {
this.attributeLongName = attributeLongName;
}
@XmlElementWrapper(name="Values")
@XmlElement(name="Value")
public List<Value> getValues() {
return values;
}
public void setValues(List<Value> values) {
this.values = values;
}
@Override
public String toString() {
Field[] fields = this.getClass().getDeclaredFields();
String res = "\n";
try {
for (Field field : fields) {
res += field.getName() + " : " + field.get(this) + "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
值:
import java.lang.reflect.Field;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
//import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Value {
String Value;
public String getValue() {
return Value;
}
public void setValue(String Value) {
this.Value = Value;
}
@Override
public String toString() {
Field[] fields = this.getClass().getDeclaredFields();
String res = "\n";
try {
for (Field field : fields) {
res += field.getName() + " : " + field.get(this) + "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
输入:
<?xml version="1.0" encoding="utf-8"?>
<Entity Id="718803" ExternalId="10000" LongName="Entity LongName">
<Attributes>
<Attribute Id="100" Name="Name1" LongName="LongName1">
<Values>
<Value>
<![CDATA[Value1]]>
</Value>
</Values>
</Attribute>
<Attribute Id="200" Name="Name2" LongName="LongName2">
<Values>
<Value>
<![CDATA[Value2]]>
</Value>
</Values>
</Attribute>
<Attribute Id="300" Name="Name3" LongName="LongName3">
<Values>
<Value>
<![CDATA[Value3]]>
</Value>
</Values>
</Attribute>
</Attributes>
</Entity>
输出:
------- XML to Object -----------
externalId : 10000
attributes : [
attributeName : Name1
attributeLongName : LongName1
values : [
Value : null
]
,
attributeName : Name2
attributeLongName : LongName2
values : [
Value : null
]
,
attributeName : Name3
attributeLongName : LongName3
values : [
Value : null
]
]
提前感谢您的帮助。
丹