有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java XJC限制将双精度转换为字符串

我通过从XSD生成带有javax.xml.bind注释的类来读取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生成了一个字符串而不是一个双精度字符串

public String getBar(){....}

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

解决方案:

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


共 (1) 个答案

  1. # 1 楼答案

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

    1. 测试。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. 文本。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. 转换器。带有转换函数的java类

      public class Converter {
        public static Double fromString(String str){
          return Double.parseDouble(str);
        }
      
        public static String toString(Double value){
          return String.valueOf(value);
        }
      }
      

    就这些。使用命令调用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,那么可能您忘记了在配置中添加一些内容。但是如果没有关于配置的详细信息,就不可能说出代码的错误