問題描述
我想使用 JAXB 2.1 XJC 從 xsd 生成 java 代碼.我提供了一個 xsd 架構(gòu),但我無法更改它.我想在從 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 或?qū)?processContents 更改為strict".這些選項現(xiàn)在對我來說都不可接受,因為我無法更改 xml 架構(gòu),而且我有一些舊代碼依賴于使用 xjc:simple 模式生成的 java 類,但現(xiàn)在我需要使用 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>
您不必為此更改架構(gòu),您可以通過綁定文件使用此自定義.
You don't have to change the schema for this, you can use this customization via binding files.
這篇關于JAXB 2.1 - 自定義 xs:any 綁定的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!