[英]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.