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

2024-01-06

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

<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 类。不幸的是,访问器Codeelement 接受一个整数列表作为参数,这意味着所有前导 0 都被去掉(因为据我所知,在使用整数类型时,没有办法在 Java 中保留前导 0)!

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

感谢您的帮助!


您可以执行以下操作:

数字格式化程序

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

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(格式.xsd)

然后,当您要从 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>

绑定.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 Call

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

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

Adapter1

这将导致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将使用以下方式从您的域对象引用@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

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

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

您将得到所需的输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <number>0000004</number>
</root>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对需要带有模式的整数的元素使用 JAXB 生成的类 的相关文章

随机推荐

  • 产品属性的数据库架构

    我想在类别中实现产品过滤 并且对正确的数据库架构有疑问 现在我有下表 类别 1 id 2 category 3 description Products 1 id 2 category id 3 product 4 image 5 pric
  • UDID 和 UUID 之间的区别[重复]

    这个问题在这里已经有答案了 有人说UDID Unique Device IDentifier 有人说UUID Universally Unique IDentifier 它们是否相同 它们之间有什么区别 UUID 通用唯一标识符 以每个应用
  • 闭包中局部变量的错误行为

    我被下面的代码困住了 首先 我将描述用例 使用 ColorGradient 实例调用函数 addPreset 打电话时this listController addItem 名为的回调函数onSelect是提供的 每次触发 listCont
  • 错误:useHref() 只能在 组件的上下文中使用

    当我直接在我的路由器组件中写入我的导航栏组件内容时 它工作正常 但是当我在导航栏组件中写入该内容时 它会生成以下错误 错误 useHref 只能在组件上下文中使用 我在用着 react dom 17 0 2 react router dom
  • 使用 clang 编译时 openmp 无法正确链接

    我已经在 Ubuntu 16 04 上从源代码构建了 clang 4 0 并尝试编译一个简单的 OpenMP 程序 但收到以下错误 tmp test 7f2c7c o In function main home me sf shared t
  • 选择两列之间的日期

    我需要一个 SQL 查询 如果我有两列STARTDATE and END DATE 我想选择日期位于这两个日期之间的所有行 例如 开始日期 1 1 2011 且结束日期 2 2 2011 SELECT FROM table1 WHERE 2
  • Laravel 4 中的通用访问器和修改器

    我知道可以为各个字段定义访问器和修改器 如下所示 public function setSomeAttribute value set the attribute public function getSomeAttribute retur
  • 如何在Java中获取客户端计算机上当前登录用户的用户名?

    当用户 客户端 尝试通过键入 URL 进入应用程序时 我需要获取该计算机的 Windows 登录用户名 我尝试过System getProperty user name 但是当我从其他机器 客户端 访问应用程序时 这仍然显示服务器上登录用户
  • 如何使用本地安装在node_modules中的包中的可执行文件?

    如何在中使用模块的本地版本node js 例如 在我的应用程序中 我安装了咖啡脚本 npm install coffee script 这会将其安装在 node modules咖啡命令是 node modules bin coffee 当我
  • 如何以编程方式从 UIView 获取约束

    我想从 UIView 获取 UILabel 约束 但我无法得到任何约束 我在 CustomView m 中设置约束 如下所示 id initWithFrame CGRect frame self super initWithFrame fr
  • 在 OpenGL3 Core Profile 中使用矩阵作为顶点属性

    我在 OSX 上使用 OpenGL 3 2 Core Profile 我想要进行实例化绘图 glDrawArraysInstanced 其中我为每个实例传递一个矩阵 我的顶点着色器构建得很好 version 150 in mediump v
  • LIKE 与 Linq to Entities

    我知道 Contains 方法确实喜欢LIKE therm the StartsWith 方法确实喜欢LIKE therm 和 EndsWith 方法就像LIKE therm but 有没有办法像下面那样做 Linq to Entities
  • Flutter iOS 崩溃并出现 EXC_BAD_ACCESS 错误

    我正在使用 Flutter 开发一个应用程序 并在物理 iOS 设备 iPhone 7 上测试该应用程序 iOS版本是 15 3 1Flutter版本是 2 10 3 当我测试我的应用程序时 偶尔会发生崩溃 崩溃给出以下错误 它并不总是在同
  • 参数索引超出范围

    尝试使用 nhibernate 更新对象时出现以下错误 我正在尝试更新一个外键字段 有什么想法为什么我可能会收到此错误 我无法从该错误中找出答案 并且我的 log4net 日志也没有给出任何提示 Thanks System IndexOut
  • 是否可以在重构文本中以两栏样式书写?

    我想使用重组文本写一篇研究论文 是否可以采用此类文档固有的两栏样式 我看了看规格 http docutils sourceforge net docs ref rst restructuredtext html但除了使用桌子这将是一个真正的
  • 检测nuxt中元素外部的点击

    我有一个 nuxt 项目 我需要编写一个 click outside 指令 通过它我可以检测元素的外部点击以关闭它们 我该如何实施 答案是在插件中创建一个directives js 文件并将其注册到config nuxt js 文件中 di
  • 使用 SWIFT 解析 PDF

    我想解析一个没有图像 只有文本的 PDF 我正在尝试寻找一些文字 例如搜索字符串 Name 并能够读取 后面的字符 我已经能够打开 PDF 获取页数并循环浏览它们 问题是当我想使用类似的函数时CGPDFDictionaryGetStream
  • Reactjs保存组件的当前状态

    我有组件 A 和组件 B 在组件 A 中 用户可以将过滤器应用于元素列表 并使用侧栏在两个组件之间导航 每当用户单击转到 B 然后返回 A 时 过滤器就会设置回初始状态 保存组件 A 状态的最佳方法是什么 以便当他们返回组件 A 时 他们可
  • 使用 PowerShell 和 Azure CLI 将代码从 GitLab 存储库部署到 Azure Web App

    我想使用 PowerShell 脚本和 Azure CLI 设置从 GitLab 存储库到 Azure 应用程序的持续部署 已经有一个答案可以使用Azure RM 模块和 Windows PowerShell https stackover
  • 对需要带有模式的整数的元素使用 JAXB 生成的类

    我的 XML 架构中有一个元素定义如下