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;
}
}
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4152467#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...