Java 如何使用 JAXB 实例化一个空元素

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

How to instantiate an empty element with JAXB

javaxmljaxb

提问by doekman

I use JAXB to create XML messages. The XML I need to create is (for the sake of simplicity):

我使用 JAXB 来创建 XML 消息。我需要创建的 XML 是(为了简单起见):

<request>
  <header/>
</request>

My code looks like this:

我的代码如下所示:

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "request")
public class Request {

    private String header;

    @XmlElement(required=true)
    public String getHeader() {
      return header;
    }

    public void setHeader(String header) {
      this.header=header;
    }
}

The problem: the headerelement is not displayed (header is null). When header is set to an empty string, the following is displayed:

问题:header元素未显示(标题为null)。当 header 设置为空字符串时,将显示以下内容:

<request>
  <header></header>
</request>

When I use as type Objectinstead of String, the result is even worse:

当我使用 as 类型Object而不是 时String,结果更糟:

<request>
  <header xsi:type="xs:string" 
   xmlns:xs="http://www.w3.org/2001/XMLSchema" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></header>
</request>

BTW: I'm using this codeto create the XML string.

顺便说一句:我正在使用此代码来创建 XML 字符串。

Is it possible to get an empty tag?

是否有可能获得一个空标签?

采纳答案by Tom Hawtin - tackline

In XML, <header/>and <header></header>are the same thing. If you really want the former, then use a prettifier. javax.xml.transform.TransformerFactory.newTransformer()will probably do that for you.

在 XML 中,<header/><header></header>是一回事。如果您真的想要前者,请使用美化剂。javax.xml.transform.TransformerFactory.newTransformer()可能会为你做到这一点。

回答by Chris Dail

An empty tag for a String object is essentially the empty string.

String 对象的空标签本质上是空字符串。

If you call the following, you will get what you are looking for:

如果您拨打以下电话,您将得到您要找的东西:

request.setHeader("")

I should also note that in XML the following two declarations of a header are idential. Both of these have no child text nodes. These are essentially the same and will be treated the same by all XML parsers:

我还应该注意,在 XML 中,标题的以下两个声明是相同的。这两个都没有子文本节点。这些本质上是相同的,并且所有 XML 解析器都将对其进行相同的处理:

<header></header>

<header/>

回答by Jin Kwon

As @Tom Hawtin - tackline said

正如@Tom Hawtin-tackline 所说

<header/>and <header></header>is same. Parsers will give you "".

<header/>并且<header></header>是一样的。解析器会给你“”。

You have to to put nillableon your header annotation

你必须把nillable你的标题注释

@XmlElement(nillable=true, required=true)
public String getHeader() {
  return header;
}

I hope this code will generate following XML for nullvalue.

我希望这段代码将生成以下 XMLnull值。

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Request {

    public static void main(String[] args) throws JAXBException {
        final Request request = new Request();
        final JAXBContext context = JAXBContext.newInstance(Request.class);
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                               Boolean.TRUE);
        marshaller.marshal(request, System.out);
        System.out.flush();
    }

    @XmlElement(nillable=true, required=true)
    private String header;
}

prints

印刷

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</request>

回答by Mark D

I wanted the same thing, effectively <header/>rather than <header></header>during the xml serialization process.

我想要同样的东西,有效地<header/>而不是<header></header>在 xml 序列化过程中。

Since a null value - rather than an empty string - will produce the correct result, I modified my setter method to set the value explicitly to null:

由于空值 - 而不是空字符串 - 会产生正确的结果,我修改了我的 setter 方法以将值显式设置为空:

public void setHeader(String header) {
    this.header = "".equals(header) ? null : header;
}