1
votes

I am using JAXB MOXy to map my java objects to XML. Up until now I have only ever needed to convert 1 object at a time and that word fine. So my output XML looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<NotificationTemplateXML template-id="1">
   <template-subject>Test Subject</template-subject>
   <template-text>Test Text</template-text>
</NotificationTemplateXML>

What I am trying to do now is to have multiple iterations of the object saved into the same XML file so it looks something like this

<?xml version="1.0" encoding="UTF-8"?>
 <NotificationTemplateXML template-id="1">
    <template-subject>Test Subject</template-subject>
    <template-text>Test Text</template-text>
 </NotificationTemplateXML>
 <NotificationTemplateXML template-id="2">
    <template-subject>Test Subject</template-subject>
    <template-text>Test Text</template-text>
 </NotificationTemplateXML>
 <NotificationTemplateXML template-id="3">
    <template-subject>Test Subject</template-subject>
    <template-text>Test Text</template-text>
 </NotificationTemplateXML>

My object mapping looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="NotificationTemplateXML")
public class NotificationTemplate {

    @XmlAttribute(name="template-id")
    private String templateId;
    @XmlElement(name="template-subject")
    private String templateSubject;
    @XmlElement(name="template-text")
    private String templateText;

Assuming I have a list of Type 'NotificationTemplate' can I simply marshall the list? Will that produce a single xml file with each of the NotificationTemplate objects as a separate 'XML object'?

NB. Likewise when I Unmarshall the XML file am I expecting to produce a list of Type 'NotificationTemplate'?

1

1 Answers

1
votes

Below are a couple different options.

OPTION #1 - PROCESS THE INVALID XML DOCUMENT

XML documents have a single root element, so your input isn't valid. Below is an answer given by @npe on how to handle this type of input:

OPTION #2 - CREATE VALID XML DOCUMENT

I would recommend converting your XML into a valid document and processing that.

XML Document With Root Element

I've modified your XML document by adding a root element.

<?xml version="1.0" encoding="UTF-8"?>
<NotificationTemplateXMLList>
    <NotificationTemplateXML template-id="1">
        <template-subject>Test Subject</template-subject>
        <template-text>Test Text</template-text>
     </NotificationTemplateXML>
     <NotificationTemplateXML template-id="2">
        <template-subject>Test Subject</template-subject>
        <template-text>Test Text</template-text>
     </NotificationTemplateXML>
     <NotificationTemplateXML template-id="3">
        <template-subject>Test Subject</template-subject>
        <template-text>Test Text</template-text>
     </NotificationTemplateXML>
<NotificationTemplateXMLList>

NotificationTemplateXMLList

I've introduced a new class to your domain model that maps to the new root element.

@XmlRootElement(name="NotificationTemplateXMLList")
@XmlAccessorType(XmlAccessType.FIELD)
public class NotificationTemplates {

     @XmlElement(name="NotificationTemplateXML")
     private List<NotificationTemplate>  notificationTemplates;
}