XJC限制将Double转换为String

时间:2013-10-22 14:37:52

标签: java xsd xjc xml-binding

我通过生成的javax.xml.bind - 来自XSD的带注释的类来读取XML。

<xsd:complexType name="foo">
  <xsd:attribute name="bar" type="xsd:double" />
</xsd:complexType>

所以我的班级生成为:

public Double getBar(){....}

好的,为确保双属性为正,我使用xsd:restriction

<xsd:simpleType name="DoublePositive">
  <xsd:restriction base="xsd:double">
    <xsd:minExclusive value="0" />
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="foo">
  <xsd:attribute name="bar" type="DoublePositive" />
</xsd:complexType>

太糟糕了,xjc会生成一个String而不是Double。

public String getBar(){....}

我可以强制使用Double而不是String吗?

解决方案:

我的问题错了。我有xsi:type="DoublePositive"而不是type="DoublePositive"

1 个答案:

答案 0 :(得分:4)

这是我的配置,我用它来生成类。

  1. test.xsd - 您的xsd文件

     <?xml version="1.0" encoding="utf-8"?>
     <xsd:schema elementFormDefault="qualified" targetNamespace=""
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
       <xsd:simpleType name="DoublePositive">
         <xsd:restriction base="xsd:double">
           <xsd:minExclusive value="0"/>
         </xsd:restriction>
       </xsd:simpleType>
    
       <xsd:complexType name="foo">
         <xsd:attribute name="bar" type="DoublePositive"/>
       </xsd:complexType>
     </xsd:schema>
    
  2. text.xml - 具有全局绑定的文件。

    <jaxb:bindings version="2.1"
                    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
       <jaxb:globalBindings generateElementProperty="false">
         <jaxb:javaType name="java.util.Double" xmlType="DoublePositive"
                        parseMethod="Converter.fromString"
                        printMethod="Converter.toString"/>
       </jaxb:globalBindings> 
     </jaxb:bindings>
    
  3. Converter.java - 具有转换函数的类。

    public class Converter {
      public static Double fromString(String str){
        return Double.parseDouble(str);
      }
    
      public static String toString(Double value){
        return String.valueOf(value);
      }
    }
    
  4. 这就是全部。使用命令

    调用xjc

    xjc -b test.xml -classpath . test.xsd

    $ xjc -version
    xjc version "JAXB 2.1.10" 
    JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build JAXB 2.1.10)
    $ xjc -b test.xml -classpath . test.xsd 
    parsing a schema... 
    [WARNING] EmptyTargetNamespace: In schema document 'file:/pwd/test.xsd', the
    value of the 'targetNamespace' attribute cannot be an empty string.  
    line 3 of file:/pwd/test.xsd
    
    compiling a schema... 
    generated/Adapter1.java
    generated/Foo.java
    generated/ObjectFactory.java 
    
    

    并生成文件内容:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "foo")
    public class Foo {
    
        @XmlAttribute
        @XmlJavaTypeAdapter(Adapter1 .class)
        protected Double bar;
    
        /**
         * Gets the value of the bar property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public Double getBar() {
            return bar;
        }
    
        /**
         * Sets the value of the bar property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setBar(Double value) {
            this.bar = value;
        }
    
    }
    

    和适配器类

    package generated;
    
    import java.util.Double;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class Adapter1
        extends XmlAdapter<String, Double>
    {
    
    
        public Double unmarshal(String value) {
            return (Converter.fromString(value));
        }
    
        public String marshal(Double value) {
            return (Converter.toString(value));
        }
    
    }
    

    因此,正如您所看到的,xjc正确生成了类。如果您仍然获得String,那么您可能忘记在配置中添加内容。但是如果没有关于配置的详细信息,就不可能说出你的代码出了什么问题。