简体   繁体   English

如何使用JAXB输出对象集合?

[英]How do I output a collection of objects with JAXB?

Not able to generate the required XML from the java class as expected. 无法按预期从java类生成所需的XML。

This is a class defining the property of zoo 这是一个定义动物园属性的类

**//Modal Class**
 public class Zoo 
{
private String name;
private String place;
    //Getters Setters

}

Action class with list of zoo class to be in XML 动作类,其中动物园类的列表使用XML

//Action Class with multiple objects of zoo class //具有Zoo类的多个对象的Action类

@ManagedBean
@XmlRootElement
public @SessionScoped class zoos implements Serializable {
   private String name;
   private String place;
   private static final ArrayList<Zoo> zoo_list
= new ArrayList<Zoo>();

    @XmlElement
public ArrayList<Zoo> getZoo_list()
{
  return zoo_list;

}
    public void xmleg()
    {

        File file = new File("C:\\file.xml");   
        for(Zoo add: zoo_list)
        {
         try 
         {
            JAXBContext jaxbContext = JAXBContext.newInstance(Zoos.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


            jaxbMarshaller.marshal(new JAXBElement<Zoo>(new QName("","Zoo"),zoo.class,add),file);
            jaxbMarshaller.marshal(new JAXBElement<Zoo>(new QName("","Zoo"),zoo.class,add),System.out);

              } catch (JAXBException e) {
            e.printStackTrace();
              }
         }
        }

Output that is generated is: 生成的输出是:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo>
    <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
</zoo>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo>
   <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
</zoo>

But the Expected Output should be: 但是预期输出应为:

 <zoos>
   <zoo>
     <linkId>0</linkId>
     <name>gfdsgdgtretr</name>
     <place>gdfg</place>
  </zoo>
  <zoo>
    <linkId>0</linkId>
    <name>gfdsgdgtretr</name>
    <place>gdfg</place>
  </zoo>     
  </zoos>

Have tried with the @XMLElementWrapper annotation as well but its not working too.Have gone through many tutorials but didnt find the solution. 也尝试过使用@XMLElementWrapper批注,但它也无法正常工作。经历了许多教程,但没有找到解决方案。

UPDATE UPDATE

I just reread your question and you already have the Zoos class. 我只是重新阅读了您的问题,您已经有Zoos课。 You should be marshalling the instance of that instead of the individual items from the zoo_list property. 您应该将其实例而不是zoo_list属性中的单个项目zoo_list You can change the element name that property maps to with the @XmlElement annotation. 您可以使用@XmlElement批注更改属性映射到的元素名称。

@XmlElement(name="zoo")
public ArrayList<Zoo> getZoo_list()

JAXB (JSR-222) implementations require a root objject. JAXB(JSR-222)实现需要一个根对象。 The easiest thing to do is create an object called Zoos that holds onto the list of Zoo objects and marshal that. 最简单的方法是创建一个名为Zoos的对象,该对象将保存在Zoo对象列表中并将其编组。

@XmlRootElement
public class Zoos {

    private List<Zoo> zoo;

    public List<Zoo> getZoo() {
        return zoo;
    }

    public void setZoo(List<Zoo> zoo) {
        this.zoo = zoo;
    }

}

Alternatively, you could create a FileWriter for the File . 或者,您可以为File创建一个FileWriter Then you could write the start element yourself. 然后,您可以自己编写start元素。 Then use JAXB to marshal each of the Zoo instances to the FileWriter (you will need to leverage the following property to have JAXB exclude the XML header. 然后使用JAXB将每个Zoo实例编组到FileWriter (您将需要利用以下属性来使JAXB排除XML标头。

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

Finally you need to close the root element. 最后,您需要关闭根元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JAXB序列化多个对象 - How do I serialize multiple objects with JAXB 如何使用JAXB将以下xml转换为Java对象 - How do I convert the following xml into java objects using JAXB 如何使用 JAXB 编组多个对象? - How do I marshall multiple objects using JAXB? JAXb对象中的推土机映射集合 - Dozer mapping collection in JAXb objects 如何将JAXB生成的集合中的All()项目添加到简单的ArrayList中? - How do I addAll() items from a JAXB-generated collection to a simple ArrayList? 我如何分开<xsd:choice />使用 JAXB 将子元素放入单独的集合属性中? - How do I separate the <xsd:choice/> sub-elements into individual Collection properties using JAXB? 如何在不使用批注的情况下使用JAXB将Java对象转换为xml? - How do I convert java objects to xml using JAXB without using annotation? 如何在Java8中将数据从对象集合复制到不同对象的集合 - How do I copy data from a collection of objects to a collection of different objects in Java8 如何将集合中的所有对象与同一集合中的所有对象进行比较? - How do I compare all objects in a collection against all objects in the same collection? 如何编写比较对象集合中的 2 个不同字段的比较器? - How do I write a comparator that compares 2 different fields in a collection of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM