java 对需要带有模式的整数的元素使用 JAXB 生成的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7182533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JAXB generated class for an element that requires an integer with a pattern
提问by Brian Schrameck
I have an element in my XML Schema that is defined as follows:
我的 XML 架构中有一个元素,其定义如下:
<xs:complexType name="MyNumberCodeType">
<xs:sequence>
<xs:element name="Code" type="NumberCodeValueType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
Where NumberCodeValueType is:
其中 NumberCodeValueType 是:
<xs:simpleType name="NumberCodeValueType">
<xs:restriction base="xs:int">
<xs:pattern value="[0-7]{7}"/>
</xs:restriction>
</xs:simpleType>
That is, my number can start with leading 0s. I can NOT modify this schema. I am using JAXB to generate my Java classes. Unfortunately, the accessor for the Code
element takes a list of integers as the argument which means all leading 0s are stripped off (because, from what I can tell, there's no way to keep leading 0s in Java when using an integer type)!
也就是说,我的号码可以以 0 开头。我不能修改这个架构。我正在使用 JAXB 来生成我的 Java 类。不幸的是,Code
元素的访问器将一个整数列表作为参数,这意味着所有前导 0 都被去除(因为,据我所知,在 Java 中使用整数类型时无法保留前导 0)!
Is there any way I can fix this?
有什么办法可以解决这个问题吗?
Thanks for your help!
谢谢你的帮助!
回答by bdoughan
You could do the following:
您可以执行以下操作:
NumberFormatter
数字格式器
You can do this by writing your own formatter:
您可以通过编写自己的格式化程序来做到这一点:
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)
XMLSchema (format.xsd)
Then when you are going to generate your classes from your XML Schema:
然后,当您要从 XML 架构生成类时:
<?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
绑定文件
You will leverage a JAXB bindings file to reference your formatter:
您将利用 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 Call
XJC呼叫
The bindings file is referenced in the XJC call as:
绑定文件在 XJC 调用中被引用为:
xjc -d out -p forum7182533 -b bindings.xml format.xsd
Adapter1
适配器 1
This will cause an XmlAdapter
to be created that leverages your formatter:
这将导致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));
}
}
Root
根
The XmlAdapter
will be referenced from your domain object using the @XmlJavaTypeAdapter
annotation:
在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;
}
}
Demo
演示
Now if you run the following demo code:
现在,如果您运行以下演示代码:
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);
}
}
Output
输出
You will get the desired output:
您将获得所需的输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<number>0000004</number>
</root>