简体   繁体   中英

How to let JAXB render boolean as 0 and 1, not true and false

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"?

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;
}

Having the same problem as user20298, I followed the hint by mtpettyp, and adapted it for my configuration.

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)

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'

This is the plugin configuration in 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:

    <?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";
    }
}

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 parseMethod and a printMethod . These methods could be as simple as

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 !

I would probably create a type adapter for converting a boolean to an int. There are some examples in the JAXB users guide.

您可以编写一对解析器/编写器,并在绑定JAXB XML中定义属性映射。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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