Question

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">&lt;AccountInfo AccountID="640480"&gt;&lt;Location LocationID="1490075"/&gt;&lt;Location LocationID="8900561"/&gt;&lt;Location LocationID="2367782"/&gt;&lt;Location LocationID="2226598"/&gt;&lt;/AccountInfo&gt;</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
Was it helpful?

Solution

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.

OTHER TIPS

Im Getting the Location List - Here is the code.

 String xml ="" + 
               "  <AccountInfo xmlns=\"http://www.cspire.com/omnia/schema\" AccountID=\"640480\"> " + 
               "    <Location LocationID=\"1490075\"/> " + 
               "    <Location LocationID=\"8900561\"/> " + 
               "    <Location LocationID=\"2367782\"/> " + 
               "    <Location LocationID=\"2226598\"/> " + 
               "  </AccountInfo> " + 
               "";

    try {

          String testXML = xml ;
          ByteArrayInputStream inputXml = new ByteArrayInputStream (testXML.getBytes());
          JAXBContext jc = JAXBContext.newInstance(AccountInfo.class);
          Unmarshaller  unmarshaller = jc.createUnmarshaller();
          AccountInfo result;
         result = (AccountInfo) unmarshaller.unmarshal(inputXml);


         System.out.println("Account ID: " + result.getAccountID());
         System.out.println(result.getLocation().size());

         for(Location loc: result.getLocation()){
             System.out.println(loc.getLocationID());
         }


    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

Output :

    Account ID: 640480
    4
    1490075

    8900561

    2367782

    2226598
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top