java 如何让 JAXB 将布尔值呈现为 0 和 1,而不是真假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/343669/
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
How to let JAXB render boolean as 0 and 1, not true and false
提问by user20298
Got a quick question. Does anyone know how to let JAXB (marshall) render boolean fields as 1 and 0 instead of printing out "true" and "false"?
有一个快速的问题。有谁知道如何让 JAXB (marshall) 将布尔字段呈现为 1 和 0 而不是打印出“true”和“false”?
回答by mtpettyp
The adapter class:
适配器类:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class BooleanAdapter extends XmlAdapter<Integer, Boolean>
{
@Override
public Boolean unmarshal( Integer s )
{
return s == null ? null : s == 1;
}
@Override
public Integer marshal( Boolean c )
{
return c == null ? null : c ? 1 : 0;
}
}
Usage:
用法:
@XmlElement( name = "enabled" )
@XmlJavaTypeAdapter( BooleanAdapter.class )
public Boolean getEnabled()
{
return enabled;
}
回答by Aim Mistaken
Having the same problem as user20298, I followed the hint by mtpettyp, and adapted it for my configuration.
遇到与 user20298 相同的问题,我按照 mtpettyp 的提示进行操作,并根据我的配置对其进行了调整。
My configuration is: - maven to build the project. - "org.jvnet.jaxb2.maven2" plugin in maven. - jaxb 2.2.6 - In this occasion, I was making Java classes for the kml 2.2 (ogckml22.xsd)
我的配置是: - maven 来构建项目。- maven 中的“org.jvnet.jaxb2.maven2”插件。- jaxb 2.2.6 - 在这种情况下,我正在为 kml 2.2 (ogckml22.xsd) 制作 Java 类
And I stumbled upon the problem of the booleans to be rendered as 'true/false', when Google maps wants them to be as '1/0'
我偶然发现了布尔值被渲染为“真/假”的问题,当谷歌地图希望它们为“1/0”时
This is the plugin configuration in maven:
这是maven中的插件配置:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generateDirectory>src/main/generated</generateDirectory>
<extension>true</extension>
<removeOldOutput>true</removeOldOutput>
</configuration>
I added to the src/main/resources folder a jaxb-bindings.xjb file with the following content:
我在 src/main/resources 文件夹中添加了一个 jaxb-bindings.xjb 文件,其内容如下:
<?xml version="1.0" ?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
version="2.1"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<globalBindings>
<xjc:simple/>
<xjc:javaType name="java.lang.Boolean"
xmlType="xsd:boolean"
adapter="path.to.my.JaxbBooleanAdapter"/>
</globalBindings>
...
...
</bindings>
The adapter class is like:
适配器类是这样的:
package path.to.my;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
* Utility class to correctly render the xml types used in JAXB.
*/
public class JaxbBooleanAdapter extends XmlAdapter<String, Boolean>
{
@Override
public Boolean unmarshal(String v) throws Exception
{
if ("1".equals(v))
{
return true;
}
return false;
}
@Override
public String marshal(Boolean v) throws Exception
{
if (v == null)
{
return null;
}
if (v)
{
return "1";
}
return "0";
}
}
回答by Luc Touraille
JAXB provides a flexible way to customize your bindings. You simply have to write an XML file that will indicate how you want to bind your XML and Java types. In your case, you could use a <javaType>declaration, in which you can specify a parseMethodand a printMethod. These methods could be as simple as
JAXB 提供了一种灵活的方式来定制您的绑定。您只需编写一个 XML 文件,该文件将指示您希望如何绑定您的 XML 和 Java 类型。在您的情况下,您可以使用<javaType>声明,您可以在其中指定 aparseMethod和 a printMethod。这些方法可能很简单
public boolean myParseBool(String s)
{
return s.equals("1");
}
public String myPrintBool(boolean b)
{
return b ? "1" : "0";
}
There might exist easier ways, maybe using DatatypeConverter, but I'm not enough aware of this subject to help you more !
可能存在更简单的方法,也许使用 DatatypeConverter,但我对这个主题的了解还不够,无法为您提供更多帮助!
回答by Guemundur Bjarni
回答by Dennis C
You can write a pair of parser/writers and define the property mapping in binding JAXB XML.
您可以编写一对解析器/编写器并在绑定 JAXB XML 中定义属性映射。

