java – 使用JAXB生成的类用于需要具有模式的整数的元素

前端之家收集整理的这篇文章主要介绍了java – 使用JAXB生成的类用于需要具有模式的整数的元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 XML Schema中有一个元素定义如下:
  1. <xs:complexType name="MyNumberCodeType">
  2. <xs:sequence>
  3. <xs:element name="Code" type="NumberCodeValueType" maxOccurs="unbounded" />
  4. </xs:sequence>
  5. </xs:complexType>

其中NumberCodeValueType是:

  1. <xs:simpleType name="NumberCodeValueType">
  2. <xs:restriction base="xs:int">
  3. <xs:pattern value="[0-7]{7}"/>
  4. </xs:restriction>
  5. </xs:simpleType>

也就是说,我的号码可以从0开始.我不能修改这个模式.我正在使用JAXB生成我的Java类.不幸的是,Code元素的访问器将一个整数列表作为参数,这意味着所有前导0都被剥离(因为从我可以看出,使用整数类型时,没有办法在Java中保持前导0)!

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

谢谢你的帮助!

解决方法

您可以执行以下操作:

的NumberFormatter

你可以通过编写你自己的格式化来做到这一点:

  1. package forum7182533;
  2.  
  3. public class NumberFormatter {
  4.  
  5. public static String printInt(Integer value) {
  6. String result = String.valueOf(value);
  7. for(int x=0,length = 7 - result.length(); x<length; x++) {
  8. result = "0" + result;
  9. }
  10. return result;
  11. }
  12.  
  13. public static Integer parseInt(String value) {
  14. return Integer.valueOf(value);
  15. }
  16.  
  17. }

XMLSchema(format.xsd)

那么当你要从你的XML Schema生成你的类时:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3.  
  4. <xs:element name="root">
  5. <xs:complexType>
  6. <xs:sequence>
  7. <xs:element name="number" type="NumberCodeValueType" />
  8. </xs:sequence>
  9. </xs:complexType>
  10. </xs:element>
  11.  
  12. <xs:simpleType name="NumberCodeValueType">
  13. <xs:restriction base="xs:int">
  14. <xs:pattern value="[0-7]{7}" />
  15. </xs:restriction>
  16. </xs:simpleType>
  17.  
  18. </xs:schema>

bindings.xml

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

  1. <jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
  2. xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
  3. <jxb:bindings schemaLocation="format.xsd">
  4. <!--jxb:bindings node="//xs:simpleType[@name='NumberCodeValueType']" -->
  5. <jxb:bindings node="//xs:element[@name='number']">
  6. <jxb:property>
  7. <jxb:baseType>
  8. <jxb:javaType name="java.lang.Integer"
  9. parseMethod="forum7182533.NumberFormatter.parseInt" printMethod="forum7182533.NumberFormatter.printInt" />
  10. </jxb:baseType>
  11. </jxb:property>
  12. </jxb:bindings>
  13. </jxb:bindings>
  14. </jxb:bindings>

XJC电话

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

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

适配器1

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

  1. package forum7182533;
  2.  
  3. import javax.xml.bind.annotation.adapters.XmlAdapter;
  4.  
  5. public class Adapter1
  6. extends XmlAdapter<String,Integer>
  7. {
  8.  
  9.  
  10. public Integer unmarshal(String value) {
  11. return (forum7182533.NumberFormatter.parseInt(value));
  12. }
  13.  
  14. public String marshal(Integer value) {
  15. return (forum7182533.NumberFormatter.printInt(value));
  16. }
  17.  
  18. }

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

  1. package forum7182533;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import javax.xml.bind.annotation.XmlType;
  8. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  9.  
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. @XmlType(name = "",propOrder = {
  12. "number"
  13. })
  14. @XmlRootElement(name = "root")
  15. public class Root {
  16.  
  17. @XmlElement(required = true,type = String.class)
  18. @XmlJavaTypeAdapter(Adapter1 .class)
  19. protected Integer number;
  20.  
  21. public Integer getNumber() {
  22. return number;
  23. }
  24.  
  25. public void setNumber(Integer value) {
  26. this.number = value;
  27. }
  28.  
  29. }

演示

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

  1. package forum7182533;
  2.  
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.Marshaller;
  5.  
  6. public class Demo {
  7.  
  8. public static void main(String[] args) throws Exception {
  9. JAXBContext jc = JAXBContext.newInstance(Root.class);
  10.  
  11. Root root = new Root();
  12. root.setNumber(4);
  13.  
  14. Marshaller marshaller = jc.createMarshaller();
  15. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  16. marshaller.marshal(root,System.out);
  17. }
  18. }

产量

您将获得所需的输出

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <root>
  3. <number>0000004</number>
  4. </root>

猜你在找的Java相关文章