Java JAXB MOXy将多个对象迭代映射到单个XML

Java JAXB MOXy将多个对象迭代映射到单个XML,java,xml,jaxb,eclipselink,moxy,Java,Xml,Jaxb,Eclipselink,Moxy,我正在使用JAXB MOXy将java对象映射到XML。到目前为止,我只需要一次转换一个对象,这个词很好。因此,我的输出XML如下所示: <?xml version="1.0" encoding="UTF-8"?> <NotificationTemplateXML template-id="1"> <template-subject>Test Subject</template-subject> <template-text>

我正在使用JAXB MOXy将java对象映射到XML。到目前为止,我只需要一次转换一个对象,这个词很好。因此,我的输出XML如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<NotificationTemplateXML template-id="1">
   <template-subject>Test Subject</template-subject>
   <template-text>Test Text</template-text>
</NotificationTemplateXML>
@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;
假设我有一个“NotificationTemplate”类型的列表,我可以简单地整理列表吗?这会产生一个单独的xml文件,其中每个NotificationTemplate对象都是一个单独的“xml对象”吗


注意。同样,当我解组XML文件时,我是否希望生成“NotificationTemplate”类型的列表?

下面是两个不同的选项

选项#1-处理无效的XML文档

XML文档只有一个根元素,因此输入无效。以下是关于如何处理此类输入的答案:

选项#2-创建有效的XML文档

我建议您将XML转换为有效文档并进行处理

具有根元素的XML文档

我通过添加根元素修改了您的XML文档

<?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>
@XmlRootElement(name="NotificationTemplateXMLList")
@XmlAccessorType(XmlAccessType.FIELD)
public class NotificationTemplates {

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

受试者
测试文本
受试者
测试文本
受试者
测试文本
通知模板XMLList

我向域模型引入了一个新类,它映射到新的根元素

<?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>
@XmlRootElement(name="NotificationTemplateXMLList")
@XmlAccessorType(XmlAccessType.FIELD)
public class NotificationTemplates {

     @XmlElement(name="NotificationTemplateXML")
     private List<NotificationTemplate>  notificationTemplates;
}
@XmlRootElement(name=“NotificationTemplateXMLList”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类通知模板{
@XmlElement(name=“NotificationTemplateXML”)
私有列表通知模板;
}

下面是几个不同的选项

选项#1-处理无效的XML文档

XML文档只有一个根元素,因此输入无效。以下是关于如何处理此类输入的答案:

选项#2-创建有效的XML文档

我建议您将XML转换为有效文档并进行处理

具有根元素的XML文档

我通过添加根元素修改了您的XML文档

<?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>
@XmlRootElement(name="NotificationTemplateXMLList")
@XmlAccessorType(XmlAccessType.FIELD)
public class NotificationTemplates {

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

受试者
测试文本
受试者
测试文本
受试者
测试文本
通知模板XMLList

我向域模型引入了一个新类,它映射到新的根元素

<?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>
@XmlRootElement(name="NotificationTemplateXMLList")
@XmlAccessorType(XmlAccessType.FIELD)
public class NotificationTemplates {

     @XmlElement(name="NotificationTemplateXML")
     private List<NotificationTemplate>  notificationTemplates;
}
@XmlRootElement(name=“NotificationTemplateXMLList”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类通知模板{
@XmlElement(name=“NotificationTemplateXML”)
私有列表通知模板;
}

如果您声明的列表中的对象不同怎么办。我有一个类似的情况:如何在JAXB中使用相同名称的多个部分?您能帮忙吗?如果您声明的列表中的对象不同怎么办。我有一个类似的情况:如何在JAXB中使用相同名称的多个部分?你能帮忙吗?