将JAXB生成的类用于需要带有模式的整数的元素

时间:2011-08-24 21:29:52

标签: java xsd jaxb

我的XML Schema中有一个元素定义如下:

<xs:complexType name="MyNumberCodeType">
    <xs:sequence>
        <xs:element name="Code" type="NumberCodeValueType" maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>

NumberCodeValueType为:

<xs:simpleType name="NumberCodeValueType">
    <xs:restriction base="xs:int">
        <xs:pattern value="[0-7]{7}"/>
    </xs:restriction>
</xs:simpleType>

也就是说,我的号码可以从前导0开始。我无法修改此架构。我正在使用JAXB生成我的Java类。不幸的是,Code元素的访问器将整数列表作为参数,这意味着所有前导0都被剥离(因为,据我所知,当使用整数时,无法在Java中保持前导0型)!

有什么方法可以解决这个问题吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:34)

您可以执行以下操作:

<强>的NumberFormatter

您可以通过编写自己的格式化程序来完成此操作:

package forum7182533;

public class NumberFormatter {

    public static String printInt(Integer value) {
        String result = String.valueOf(value);
        for(int x=0, length = 7 - result.length(); x<length; x++) {
            result = "0" + result;
        }
        return result;
    }

    public static Integer parseInt(String value) {
        return Integer.valueOf(value);
    }

}

XMLSchema(format.xsd)

然后,当您要从XML Schema生成类时:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="number" type="NumberCodeValueType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="NumberCodeValueType">
        <xs:restriction base="xs:int">
            <xs:pattern value="[0-7]{7}" />
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

<强> bindings.xml

您将利用JAXB绑定文件来引用格式化程序:

<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
    <jxb:bindings schemaLocation="format.xsd">
        <!--jxb:bindings node="//xs:simpleType[@name='NumberCodeValueType']" -->
        <jxb:bindings node="//xs:element[@name='number']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.Integer"
                        parseMethod="forum7182533.NumberFormatter.parseInt" printMethod="forum7182533.NumberFormatter.printInt" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC致电

绑定文件在XJC调用中被引用为:

xjc -d out -p forum7182533 -b bindings.xml format.xsd

<强>适配器1

这将导致创建XmlAdapter以利用您的格式化程序:

package forum7182533;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class Adapter1
    extends XmlAdapter<String, Integer>
{


    public Integer unmarshal(String value) {
        return (forum7182533.NumberFormatter.parseInt(value));
    }

    public String marshal(Integer value) {
        return (forum7182533.NumberFormatter.printInt(value));
    }

}

<强>根

XmlAdapter将使用@XmlJavaTypeAdapter注释从您的域对象中引用:

package forum7182533;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "number"
})
@XmlRootElement(name = "root")
public class Root {

    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(Adapter1 .class)
    protected Integer number;

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer value) {
        this.number = value;
    }

}

<强>演示

现在,如果您运行以下演示代码:

package forum7182533;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.setNumber(4);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}

<强>输出

您将获得所需的输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <number>0000004</number>
</root>