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

前端 未结 5 1600
死守一世寂寞
死守一世寂寞 2020-12-08 19:53

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

相关标签:
5条回答
  • 2020-12-08 20:21

    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 !

    0 讨论(0)
  • 2020-12-08 20:26

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 20:35

    You can write a pair of parser/writers and define the property mapping in binding JAXB XML.

    0 讨论(0)
  • 2020-12-08 20:41

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

    0 讨论(0)
  • 2020-12-08 20:43

    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";
        }
    }
    
    0 讨论(0)
提交回复
热议问题