JAXB 2.1 - 自定义xs:任何绑定 [英] JAXB 2.1 - Customize xs:any binding

查看:189
本文介绍了JAXB 2.1 - 自定义xs:任何绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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作为java.lang.Object的任何元素的唯一方法是在xsd中删除 xjc:simple 或将 processContents更改为strict。这些选项现在对我来说都不可接受,因为我无法更改xml架构,并且我有一些依赖于使用xjc生成的java类的遗留代码:简单模式但现在我需要使用xs:any element而我想避免使用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的通配符+插件rel =noreferrer>通配符插件 。这允许您自定义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:任何绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆