I'm using JAXB2 to unmarshal my XML string into a java object called AccountInfo. AccountInfo contains an accountID and a list of Location objects. Currently I'm able to pull the acountID from the xml but my location list is always null. Any help would be greatly appreciated! Gracias!
Here's my xml I'm attempting to unmarshal:
<AccountInfo AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
My AccountInfo schema (which automatically generates my java objects):
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.cspire.com/omnia/schema" xmlns:tns="http://www.cspire.com/omnia/schema"
elementFormDefault="qualified">
<xsd:element name="AccountInfo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Location" type="tns:Location" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="AccountID" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Location">
<xsd:attribute name="LocationID" type="xsd:string" />
</xsd:complexType>
</schema>
(partial) AccountInfo.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"location"})
@XmlRootElement(name = "AccountInfo")
public class AccountInfo implements Equals, HashCode, ToString
{
@XmlElement(name = "Location")
protected List<Location> location;
@XmlAttribute(name = "AccountID")
protected String accountID;
public AccountInfo() {
super();
}
public AccountInfo(final List<Location> location, final String accountID) {
this.location = location;
this.accountID = accountID;
}
public List<Location> getLocation() {
if (location == null) {
location = new ArrayList<Location>();
}
return this.location;
}
public String getAccountID() {
return accountID;
}
public void setAccountID(String value) {
this.accountID = value;
}
(partial) Location.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Location")
public class Location implements Equals, HashCode, ToString
{
@XmlAttribute(name = "LocationID")
protected String locationID;
public Location() {
super();
}
public Location(final String locationID) {
this.locationID = locationID;
}
public String getLocationID() {
return locationID;
}
public void setLocationID(String value) {
this.locationID = value;
}
My main object:
object Main extends App {
val xml = <string xmlns="http://schemas.martin-group.com/openomnia"><AccountInfo AccountID="640480"><Location LocationID="1490075"/><Location LocationID="8900561"/><Location LocationID="2367782"/><Location LocationID="2226598"/></AccountInfo></string>
val testXML = xml \\ "string" text
val jc = JAXBContext.newInstance(classOf[AccountInfo])
val unmarshaller = jc.createUnmarshaller
val result = unmarshaller.unmarshal(new StreamSource(new StringReader(testXML), classOf[AccountInfo]).getValue
println(result)
println("Account ID: " + result.getAccountID)
println(result.getLocation.isEmpty)
}
Result:
com.cspire.omnia.schema.AccountInfo@74c3d5ab[location=<null>, accountID=640480]
Account ID: 640480
true
The document are trying to unmarshal does not match your XML schema.
<AccountInfo AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
To make it match the XML schema (see the targetNamespace
and elementFormDefault
attributes) and your JAXB mappings (see @XmlSchema
annotation on the package-info
class) you need to declare the namespace information.
<AccountInfo xmlns="http://www.cspire.com/omnia/schema" AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
Debugging Tip
When you can't get JAXB to unmarshal an XML document, try populating the object model and marshalling it to see the XML document that JAXB is expecting.