JAXB 2.1 - Customize xs:any binding(JAXB 2.1 - 自定义 xs:any 绑定)
问题描述
我想使用 JAXB 2.1 XJC 从 xsd 生成 java 代码.我提供了一个 xsd 架构,但我无法更改它.我想在从 xml 模式生成 java 类时使用 xjc:simple 模式.
I want to generate java code from xsd using JAXB 2.1 XJC. I have an xsd schema provided and I can't change it. I would like to use xjc:simple mode while generating java classes from xml schema.
在xsd中有元素:
<xs:any namespace="##other" processContents="lax"/>
正如这里所说:http://jaxb.java.net/guide/Mapping_of__xs_any___.html我预计这些元素将被绑定到:
As it is stated here: http://jaxb.java.net/guide/Mapping_of__xs_any___.html I expected that these elements will be binded to:
@XmlAnyElement(lax=true)
public Object any;
但是当我使用简单绑定模式时 xjc:simple 我有:
but when I use simple binding mode xjc:simple I have:
@XmlAnyElement
protected Element any;
我试图找到一种解决方法,但到处都说 xs:any 是在没有配置的情况下处理的.将 xs:any 元素作为 java.lang.Object 的唯一方法是在 xsd 中删除 xjc:simple 或将 processContents 更改为strict".这些选项现在对我来说都不可接受,因为我无法更改 xml 架构,而且我有一些旧代码依赖于使用 xjc:simple 模式生成的 java 类,但现在我需要使用 xs:any 元素,我想避免使用 org.w3c.dom.Element 对象.
I was trying to find a workaround, but everywhere it's said that xs:any is handled with no configuration. The only way to have xs:any element as java.lang.Object is to drop xjc:simple or change processContents to "strict" in xsd. None of these options are acceptable right now for me as I can't change xml schema and I have some legacy code that depends on java classes generated with xjc:simple mode but now I need to use xs:any element and I would like to avoid using org.w3c.dom.Element objects.
任何帮助将不胜感激.谢谢.
Any help would be very appreciated. Thanks.
推荐答案
你可以使用 JAXB2 Basics 的通配符插件.这允许您自定义 lax/skip/strict 通配符绑定模式:
You can use the Wildcard plugin from JAXB2 Basics. This allows you to customize lax/skip/strict wildcard binding modes:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wildcard="http://jaxb2-commons.dev.java.net/basic/wildcard"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="wildcard">
...
<xs:complexType name="issueJIIB10Type" mixed="true">
<xs:annotation>
<xs:appinfo>
<wildcard:lax/>
</xs:appinfo>
</xs:annotation>
<xs:complexContent mixed="true">
<xs:extension base="xs:anyType"/>
</xs:complexContent>
</xs:complexType>
...
</xs:schema>
您不必为此更改架构,您可以通过绑定文件使用此自定义.
You don't have to change the schema for this, you can use this customization via binding files.
这篇关于JAXB 2.1 - 自定义 xs:any 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAXB 2.1 - 自定义 xs:any 绑定
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01