I am working on a Java application to read an XML file generated out of a third party application. I am able to read the XML Attributes without an issue, but when I try to map the value of an element, it returns a null object. I am sure I am missing something simple, but haven't been able to figure it out.
Entity:
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;
}
}
Attribute:
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;
}
}
Value:
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;
}
}
Input:
<?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>
Output:
------- 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
]
]
Thanks in advance for your help.
Dan