JAXB 2.1 - 自定义 xs:any 绑定

JAXB 2.1 - Customize xs:any binding(JAXB 2.1 - 自定义 xs:any 绑定)

本文介绍了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 绑定

基础教程推荐