users@jaxb.java.net

Re: JAXB beginner questions

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 3 Mar 2009 11:05:19 +0100

To skip the <?xml...?>, you set this property
 myMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE );

This is the standard pattern for an element containing an
unlimited number of elements of type Task.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TaskListType", propOrder = {
    "tasks"
})
public class TaskListType {

    @XmlElement(name = "Tasks", required = true)
    protected List<Task> tasks;

    public List<Task> getTasks() {
        if (tasks == null) {
            tasks = new ArrayList<Task>();
        }
        return this.task;
    }
}

You should extend your ObjectFactory.java:

   private final static QName _Tasks_QNAME = new QName("", "Tasks");
    @XmlElementDecl(namespace = "", name = "Tasks")
    public JAXBElement<TaskListType> createTasks(TasksListType value) {
        return new JAXBElement<TaskListType>(_Tasks_QNAME,
TaskListType.class, null, value);
    }


For marshalling, you create a TaskListType taskList, add your stuff,
and marshal:

   JAXBElement<TaskListType> tl = of.Tasks( taskList );
   JAXBContext jc = JAXBContext.newInstance( "..." );
   Marshaller m = jc.createMarshaller();
   m.marshal( tl, System.out );

And then you probably won't need the FRAGMENT property setting.

-W



On Tue, Mar 3, 2009 at 10:40 AM, joshua1970 <joshua1970j_at_libero.it> wrote:

>
> Hi all !
> I would like to use JAXB to marshal my Entity Beans into XML. For my needs
> it's enough the use of @XmlRoot element and @XmlElement however I need to
> change a few things in the XML produced: this is a piece of the class:
>
> @XmlRootElement()
> public class Task {
>
> @XmlElement
> getAccountProductId() {..}
> @XmlElement
> getActivityStatus() {..}
> ....
>
> }
>
> and this is the xml produced:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>
> <Task><accountProductId>16EX0M</accountProductId><activityStatus>READY</activityStatus></task>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>
> <Task><accountProductId>15EJ0M</accountProductId><activityStatus>ABORT</activityStatus></task>
>
> I need to remove the <?xml > tag at the beginning and I would need to
> insert
> all Task objects inside a wrapper tag like: <TaskList> ...</TaskList>
> How can I do this if I have already set Task as root element?
> thanks a lot
> frank
> --
> View this message in context:
> http://www.nabble.com/JAXB-beginner-questions-tp22305335p22305335.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>