Asela
Asela

Reputation: 117

Unmarshalling in JAXB

I have the following tester.xml file which contains some info about events.

<?xml version="1.0"?>

<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
    <field name="esid">539661</field>
    <field name="esname">Title 01</field>
    <field name="eslink">http://www.some_event_link.com</field>
    <field name="estext">Event description 01</field>
    <field name="esinfo" xsi:nil="true" />
    <field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
    <field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
    <field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
    <field name="esbegin">2000-11-22</field>
    <field name="esend">2011-12-15</field>
    <field name="eventid">1379305</field>
    <field name="eventname">Event name 01</field>
    <field name="eventdate">2011-10-12</field>
    <field name="eventtime">19:00:00</field>
    <field name="eventlink">http://www.mysite.com/tickets.html</field>
    <field name="eventvenue">Event venue 01</field>
</row>
<row>
    <field name="esid">539636</field>
    <field name="esname">Title 02</field>
    <field name="eslink">http://www.some_event_link.com</field>
    <field name="estext">Event description 02</field>
    <field name="esinfo" xsi:nil="true" />
    <field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
    <field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
    <field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
    <field name="esbegin">2000-10-10</field>
    <field name="esend">2011-11-01</field>
    <field name="eventid">1379081</field>
    <field name="eventname">Event name 01</field>
    <field name="eventdate">2011-10-12</field>
    <field name="eventtime">14:00:00</field>
    <field name="eventlink">http://www.mysite.com/tickets.html</field>
    <field name="eventvenue">Event venue 02</field>
</row>

Also I have my XML mapping classes as follows.
First is the class corresponding to the tag < resultset >

package com.wapice.xml.beans;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "resultset")
 public class Resultset {

  private ArrayList<Row> rowsList;


  @XmlElement(required = true, name = "row")
  public ArrayList<Row> getRowsList() {
    return rowsList;
  }


  public void setRowsList(ArrayList<Row> rowsList) {
    this.rowsList = rowsList;
  }

}

Next is the class which corresponds to the tag < row >

package com.wapice.xml.beans;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlRootElement(name = "row")
@XmlType(propOrder = {"field"})
public class Row {

    private String field;


    @XmlElement(required = true, name = "field")
    public String getField() {
      return field;
    }


    public void setField(String field) {
      this.field = field;
    }

}

I tried to Unmarshall this xml into objects & print the field names & values on my console with the following code fragment.

try {
    JAXBContext context = JAXBContext.newInstance(Resultset.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Resultset resultSet = (Resultset)unmarshaller.unmarshal(new FileReader("tester.xml"));

    for(Row row : resultSet.getRowsList()){
    System.out.println("Field : " +row.getField());
    }
} 
catch (JAXBException e) {
    e.printStackTrace();
} 
catch (IOException e) {
    e.printStackTrace();
}

But when I run the above code, it only prints the last field's value. The output is as follows.

Field : Event venue 01
Field : Event venue 02

Can someone please tell me what I'm doing wrong here & it would be much appreciated if someone could tell me how to print all my < field > along with their names & values.

Thanks in advance.
Asela.

Upvotes: 2

Views: 2202

Answers (2)

Asela
Asela

Reputation: 117

I managed to solve my issue with your post & it was really really helpful. Thank you so very much for that. However I had to do some modifications to get it working as my mapping classes were throwing the following exceptions.

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "rowsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Resultset.getRowsList()
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
Class has two properties of the same name "fieldsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Row.getFieldsList()
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Row.fieldsList
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset

Then I changed the names of the related getters/setters & it worked fine. Following is how I changed it.

----------------
Class Resultset
----------------

@XmlElement(required = true, name = "row")
private ArrayList<Row> rowsList; // I kept the same name for this attribute


public ArrayList<Row> getRowsList() { // I changed this to getRows()
return rowsList;
}


public void setRowsList(ArrayList<Row> rowsList) { // I changed this to setRows()
this.rowsList = rowsList;
}


----------
Class Row
----------

@XmlElement(required = true, name = "field")
private ArrayList<Field> fieldsList;  // I kept the same name for this attribute


public void setFieldsList(ArrayList<Field> fieldsList) { // I changed this to getFields()
this.fieldsList = fieldsList;
}

public ArrayList<Field> getFieldsList() { // I changed this to setFields()
return fieldsList;
}

Hope this helps someone else too.

Upvotes: 2

bdoughan
bdoughan

Reputation: 148977

You could introduce a Field object:

package com.wapice.xml.beans;

import javax.xml.bind.annotation.*;

public class Field {
    @XmlAttribute name;
    @XmlValue value;
}

And have the Row object hold onto a List of them:

package com.wapice.xml.beans;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlRootElement(name = "row")
@XmlType(propOrder = {"field"})
public class Row {

    private List<Field> fields;

    @XmlElement(required = true, name = "field")
    public List<Field> getFields() {
      return field;
    }

    public void setField(List<Field> fields) {
      this.fields = fields;
    }

}

For More Information

Upvotes: 2

Related Questions