1 Reply Latest reply on May 21, 2008 3:20 PM by Ben Sisson

    Marshall Object List

    Ben Sisson Newbie

      I'm trying to marshall a list containing objects into an XML string. I was able to marshall a single object into an XML string but am struggling on what is needed to do a list?

      I am including an example using JBoss Tools Web Services. It will query the database and receive a back a list of WebCalendar objects. If I create a single object and marshall it will work. However, I need to marshall the list so that it creates multiple entries for each object in the marshalled XML. If you can advise I would appreciate it.

      Here is an example code:

      This is the class that will query the database which returns a list of WebPayCalendar objects I then try to marshall the list into an XML string:

      @WebMethod
      public String retrievePsWebPayCalendar()
      {
      try
      {
      /**************************************************************************
      * The following example marshals an object class into an XML string which
      * is returned to the caller. The object is an entity representing a
      * row on the database.
      **************************************************************************/
      WebPayCalendarList selectCalendar = (WebPayCalendarList) Component.getInstance("webPayCalendarList");
      // Retrieve the entire list of web pay calendar dates
      List pc = selectCalendar.getResultList();

      // For a test retrieve only the first web calendar date object
      WebPayCalendar wpc = pc.get(0);

      // Marshal the calendar information into an XML string and return it
      JAXBContext context = JAXBContext.newInstance(WebPayCalendar.class);
      Marshaller marshaller = context.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(pc, System.out);

      return result.getWriter().toString();

      }
      catch (Exception e)
      {
      return "Error: " + e.getMessage();
      }

      }

      This is the object Class:
      package org.domain.PSWebService.entity;

      import java.io.Serializable;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.UniqueConstraint;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlType;

      import org.hibernate.validator.Length;

      @Entity
      @Table(name="WEB_PAY_CALENDAR", uniqueConstraints = {@UniqueConstraint(columnNames={"RUN_ID"})})
      @XmlRootElement
      public class WebPayCalendar implements Serializable
      {

      //WEB_PAY_CALENDAR Data Columns
      @Length(max=3)
      private String payGroup;
      @Id
      @Length(max=10)
      @Column(name="RUN_ID")
      private String runId;
      @Column(name="CHECK_DT")
      private java.util.Date checkDt;
      @Column(name="PAY_BEGIN_DT")
      private java.util.Date payBeginDt;
      @Column(name="PAY_END_DT")
      private java.util.Date payEndDt;
      @Length(max=1)
      @Column(name="PAY_CONFIRM_RUN")
      private String payConfirmRun;


      public String getPayGroup()
      {
      return payGroup;
      }
      public void setPayGroup(String payGroup)
      {
      this.payGroup = payGroup;
      }
      public String getRunId()
      {
      return runId;
      }
      public void setRunId(String runId)
      {
      this.runId = runId;
      }
      public java.util.Date getCheckDt()
      {
      return checkDt;
      }
      public void setCheckDt(java.util.Date checkDt)
      {
      this.checkDt = checkDt;
      }
      public java.util.Date getPayBeginDt()
      {
      return payBeginDt;
      }
      public void setPayBeginDt(java.util.Date payBeginDt)
      {
      this.payBeginDt = payBeginDt;
      }
      public java.util.Date getPayEndDt()
      {
      return payEndDt;
      }
      public void setPayEndDt(java.util.Date payEndDt)
      {
      this.payEndDt = payEndDt;
      }
      public String getPayConfirmRun()
      {
      return payConfirmRun;
      }
      public void setPayConfirmRun(String payConfirmRun)
      {
      this.payConfirmRun = payConfirmRun;
      }

      }

        • 1. Re: Marshall Object List
          Ben Sisson Newbie

          I was able to figure out and here is sample code that produces a marshalled XML output string from objects and object lists.

          Here is the testing Class (Used to create the object instances and then marshall them):

          package org.xmlmarshalling2.com;

          import java.util.List;

          import javax.xml.bind.JAXBContext;
          import javax.xml.bind.Marshaller;

          public class RunItemTest
          {
          public static void main(String[] args)
          {
          System.out.println("test");

          Items todo = new Items();

          todo.setTitle("Beginning!!!");

          List items = todo.getItem();

          Item i1 = new Item();
          i1.setId("001");
          i1.setName("Dog");
          i1.setPrice("$3.99");

          items.add(i1);

          Item i2 = new Item();
          i2.setId("002");
          i2.setName("Cat");
          i2.setPrice("$2.99");

          items.add(i2);

          Item i3 = new Item();
          i3.setId("003");
          i3.setName("Fish");
          i3.setPrice("$1.99");

          items.add(i3);

          try
          {
          JAXBContext context = JAXBContext.newInstance(Items.class);
          Marshaller marshaller = context.createMarshaller();
          marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
          marshaller.marshal(todo, System.out);
          }
          catch (Exception e)
          {
          System.out.println("Error: " + e.getMessage());

          }

          }

          }

          Here is a base class (this class contains a reference to the object list):

          package org.xmlmarshalling2.com;

          import java.util.*;
          import javax.xml.bind.annotation.*;

          @XmlAccessorType(XmlAccessType.FIELD)
          @XmlType(name = "", propOrder = {
          "title", "item"
          })
          @XmlRootElement(name = "Items")
          public class Items {

          protected String title;

          @XmlElement(name = "Item")
          protected List item;

          public List getItem()
          {
          if (item == null) {
          item = new ArrayList();
          }
          return this.item;
          }

          public String getTitle()
          {
          return title;
          }

          public void setTitle(String title)
          {
          this.title = title;
          }


          }


          Here is the class that the object list is created from:

          package org.xmlmarshalling2.com;

          import javax.xml.bind.annotation.*;
          import javax.xml.bind.annotation.adapters.*;

          @XmlAccessorType(XmlAccessType.FIELD)
          @XmlType(name = "", propOrder = {
          "name",
          "price"
          })
          @XmlRootElement(name = "Item")
          public class Item {

          @XmlElement(name = "Name")
          protected String name;
          @XmlElement(name = "Price")
          protected String price;
          @XmlAttribute(required = true)
          @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
          protected String id;

          public String getName() {
          return name;
          }

          public void setName(String value) {
          this.name = value;
          }

          public String getPrice() {
          return price;
          }

          public void setPrice(String value) {
          this.price = value;
          }

          public String getId() {
          return id;
          }

          public void setId(String value) {
          this.id = value;
          }
          }