使用 Jackson XML 映射器将 Java 列表序列化为 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27143936/
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-08-11 03:53:47  来源:igfitidea点击:

Serialize Java List to XML using Hymanson XML mapper

javaxmlHymansonpojoxmlmapper

提问by Sharmistha Sinha

Hi I need to create an XML from JAVA using Hymanson-dataformat XMLMapper. The XML should be like

嗨,我需要使用 Hymanson-dataformat XMLMapper 从 JAVA 创建一个 XML。XML 应该像

<Customer>
  <id>1</id>
  <name>Mighty Pulpo</name>
    <addresses>
      <city>austin</city>
      <state>TX</state>
    </addresses>
    <addresses>
      <city>Hong Kong</city>
      <state>Hong Kong</state>
    </addresses>
</Customer>

But I get it always like with an extra "< addresses> < /addresses>" tag.

但我总是喜欢使用额外的“<addresses> </addresses>”标签。

<Customer>
  <id>1</id>
  <name>Mighty Pulpo</name>
<addresses>
    <addresses>
      <city>austin</city>
      <state>TX</state>
    </addresses>
    <addresses>
      <city>Hong Kong</city>
      <state>Hong Kong</state>
    </addresses>
<addresses>
</Customer>

I am using below code to create XML

我使用下面的代码来创建 XML

JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(jaxbAnnotationModule);
mapper.registerModule(new GuavaModule());
String xml = mapper.writeValueAsString(customer);
System.out.println(xml);

Please can some one help me? How can I remove the extra tag please. I have tried to use @XmlElement but it does not help help. TIA!!

请有人可以帮助我吗?请问如何删除多余的标签。我曾尝试使用 @XmlElement 但它无济于事。蒂亚!!

采纳答案by ManojP

Try the below code

试试下面的代码

@HymansonXmlRootElement(localName = "customer") 
class Customer {

    @HymansonXmlProperty(localName = "id")
    private int id;
    @HymansonXmlProperty(localName = "name")
    private String  name;

    @HymansonXmlProperty(localName = "addresses")
    @HymansonXmlElementWrapper(useWrapping = false)
    private Address[] address;

    //getters, setters, toString
}

class Address {

    @HymansonXmlProperty(localName = "city")
    private String city;

    @HymansonXmlProperty(localName = "state")
    private String state;
    // getter/setter 
}

回答by alphathesis

Just to add to ManojP's answer, if adding the @HymansonXmlElementWrapper(useWrapping = false)on the declaration of your variable doesn't work (which was the case for me), adding it to the getter method will do the trick.

只是为了添加到 ManojP 的答案中,如果@HymansonXmlElementWrapper(useWrapping = false)在变量的声明中添加不起作用(对我来说就是这种情况),将它添加到 getter 方法就行了。

回答by A Kunin

This setting changes default wrapping behavior, if you don't want to deal with annotation everywhere in your code.

如果您不想在代码中处处处理注释,则此设置会更改默认包装行为。

XmlMapper mapper = new XmlMapper();
mapper.setDefaultUseWrapper(false);