简体   繁体   English

如何将POJO列表转换为XML元素

[英]How to convert a List of POJOs to XML Elements

I'm using Spring Boot and I want to convert a POJO to XML. 我正在使用Spring Boot,并且想将POJO转换为XML。 What is the Simplest way of doing it? 最简单的方法是什么?

For example, I have a Person POJO: 例如,我有一个Person POJO:

public class Person {
  private String firstName;
  private String lastName;
  //getters/setters
}

How do I convert a List<Person> to this: 如何将List<Person>转换为此:

<rootElement>
  <person>
    <firstName>John</firstName>
    <lastName>Smith</lastName>
  </person>
</rootElement>

And what class should I use to encapsulate it in? 我应该使用哪个类来封装它? The equivalent for Jackson is JsonNode from com.fasterxml.jackson.databind package. 杰克逊相当于是JsonNodecom.fasterxml.jackson.databind包。 Are there any preconfigured beans I can use from Spring Boot? 我可以从Spring Boot中使用任何预配置的bean吗?

Manually 手动

You can use the mentioned Jackson library with XML dataformat : 您可以将提到的Jackson库与XML数据格式一起使用

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8'

Serializing: 连载:

Person person = new Person("Ima", "Person")
XmlMapper xmlMapper = new XmlMapper();
String personXml = xmlMapper.writeValueAsString(person);

Deserializing: 反序列化:

XmlMapper xmlMapper = new XmlMapper();
Person person = xmlMapper.readValue(personXml, SimpleBean.class);

Via the REST API 通过REST API

I leave this section here as it might be relevant to others who use SpringBoot for a web server: 我将本节保留在此处,因为这可能与将SpringBoot用于Web服务器的其他人有关:

Or if you are using the standard spring-boot-starter-web and want to serve the output XML through a REST API, then Spring will automatically do the conversion for you. 或者,如果您正在使用标准的spring-boot-starter-web并希望通过REST API提供输出XML,那么Spring将自动为您进行转换。 eg.the Person return type of this method means that Spring will automatically handle the conversion and transport for the output of personService.findById(id) 例如,此方法的Person返回类型意味着Spring将自动处理personService.findById(id)输出的转换和传输

 @GetMapping("/person")
public Person getPerson(@RequestParam("id") String id) {
    return personService.findById(id);
}

By default it will serve your payload objects in JSON format but you can change it to XML by adding the above dependency for the Jackson XML data format 默认情况下,它将以JSON格式提供有效载荷对象,但是您可以通过添加上述对Jackson XML数据格式的依赖关系,将其更改为XML

And additionally setting the Accept type in the request header as Application/XML 并在请求标头中另外将Accept类型设置为Application / XML

For converting a list into xml directly , I use javax.xml.bind.marshaller . 为了将列表直接转换为xml ,我使用javax.xml.bind.marshaller

You can annotate your pojo class as below 您可以如下注释您的pojo类

@XmlRootElement("Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    private String firstName;
    private String lastName;
    //getters/setters
}

And make a List class wrapping it. 并制作一个包装它的List类。

@XmlRootElement(name = "Persons_List")
public class Persons_List {

    List<Person> persons;
    // Getters and Setters

} }

And you can use Jaxb in your method as below. 您可以在下面的方法中使用Jaxb。

List<Person> persons = new List<Person>();
// add Person elements to it.
persons.add(person1);
persons.add(person2);

Persons_List persons_list = new Persons_List();
persons_list.setPersons(persons);

JAXBContext context = JAXBContext.newInstance(Persons_List.class, Person.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//if you want to output to file.
OutputStream os = new FileOutputStream( "Person.xml" );
jaxbMarshaller.marshal(persons_list, os);

//if you want to display in console.
 jaxbMarshaller.marshal(persons_list,new PrintWriter(System.out));

Output will be: 输出将是:

<Persons_List>
   <Person>
       <firstName>John</firstName>
       <lastName>Smith</lastName>
   </Person>
   <Person>
       <firstName>Will</firstName>
       <lastName>Smith</lastName>
   </Person>
</Persons_List>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM