如何从 Java 中的对象列表创建 XML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11469745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:14:43  来源:igfitidea点击:

How to create XML file from a list of objects in Java?

javaxmlserialization

提问by Pranav

I want to create one XML file from one list of objects. Objects are having some attributes, so the tags will be the attribute names and the respective data will be inside the tag.

我想从一个对象列表创建一个 XML 文件。对象有一些属性,所以标签将是属性名称,相应的数据将在标签内。

This is example:

这是示例:

I have one List myEquipmentList, that contains 100 objects of the class Equipment. Now, the attributes in the class of Equipmentare id, name, size, measures, unit_of_measure etc.

我有一个 List myEquipmentList,其中包含 100 个类的对象Equipment。现在,类中的属性Equipment是 id、name、size、measures、unit_of_measure 等。

Now I want to create XML which will be something like this.

现在我想创建类似于这样的 XML。

<Equipment id=1>``
<name>Ruler</name>
<size>1000</size>
<measures>length</measures>
<unit_of_measure>meter</unit_of_measure>
</Equipment>

Any ideas?

有任何想法吗?

采纳答案by Pita

you can create a class with the list of objects, then serialise the list to xml and finally deserialise xml to a list.

您可以使用对象列表创建一个类,然后将列表序列化为 xml,最后将 xml 反序列化为列表。

Please see this link - Very useful: How to convert List of Object to XML doc using XStream

请参阅此链接 - 非常有用: 如何使用 XStream 将对象列表转换为 XML 文档

回答by MattR

Read about JAXB.

阅读有关 JAXB 的信息。

You could have a class like this that would generate the XML you want:

你可以有一个这样的类来生成你想要的 XML:

@XmlRootElement
public class Equipment {
  private Long id;
  private String name;
  private Integer size;
  ...etc...

  @XmlAttribute
  public Long getId() {
     return id;
  }

  public void setId(Long id) {
     this.id = id;
  }

  @XmlElement
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  ... etc...

}

You'll find plenty of info on JAXB on google on searching on stackoverflow.

您会在 google 上通过搜索 stackoverflow 找到有关 JAXB 的大量信息。

http://jaxb.java.net/

http://jaxb.java.net/

http://jaxb.java.net/tutorial/

http://jaxb.java.net/tutorial/

These look like a couple of simple tutorials:

这些看起来像几个简单的教程:

http://www.mkyong.com/java/jaxb-hello-world-example/

http://www.mkyong.com/java/jaxb-hello-world-example/

http://www.vogella.com/articles/JAXB/article.html

http://www.vogella.com/articles/JAXB/article.html

回答by Francis Upton IV

One of the easiest ways to do this is simply iterate over the list and use strings to write the XML. Nothing special, very quick and easy.

执行此操作的最简单方法之一是简单地遍历列表并使用字符串来编写 XML。没什么特别的,非常快速和容易。

回答by arooaroo

I tend to use a library called Simple XML Serializationover JAXB, and I have to say it's pretty simple, yet extremely flexible.

我倾向于使用名为Simple XML Serializationover JAXB的库,我不得不说它非常简单,但非常灵活。

There's good comparison between Simple and JAXB here.

Simple 和 JAXB 之间有很好的比较here