2016-06-29 32 views
1

我有一個預定義的xsd模式(我不能修改),我喜歡通過JAXB生成相應的JAVA類。目前我正在努力處理一個複雜的類型,定義如下。JAXB從任何類型訪問字符串內容

<xsd:complexType name="AttributeType"> 
    <xsd:complexContent> 
     <xsd:extension base="xsd:anyType"> 
     <xsd:attribute name="id" type="xsd:anyURI" use="required"/> 
     <xsd:anyAttribute processContents="lax"/> 
     </xsd:extension> 
    </xsd:complexContent> 
    </xsd:complexType> 

提供的XML實例中,允許直接串的內容,這樣的:

<attribute id="myValue">201</attribute> 

以及嵌入XML這樣的:

<attribute id="address"> 
    <example:Address xmlns:example="http://example.com/ns"> 
     <Street>100 Nowhere Street</Street> 
     <City>Fancy</City> 
     <State>DC</State> 
     <Zip>99999</Zip> 
    </example:Address> 
</attribute> 

當運行XJC過程無需進一步結合修改,我得到這樣一個類:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "AttributeType", propOrder = { 
    "any" 
}) 
public class AttributeType { 

    @XmlAnyElement 
    protected List<Element> any; 
    @XmlAttribute(name = "id", required = true) 
    @XmlSchemaType(name = "anyURI") 
    protected String id; 
    @XmlAnyAttribute 
    private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

    // getter setter omitted 
} 

問題在於,我無法獲取第一個示例的字符串內容。這可能會引用XSD anytype and JAXB,但實際上我不知道如何在不修改XSD的情況下實現此目的。因此,我如何獲得字符串內容?順便說一句。我使用maven cxf-codegen插件來生成源代碼。

+0

你能解決問題嗎?引用的鏈接[鏈接](https://stackoverflow.com/questions/3488141/xsd-anytype-and-jaxb)是完全相同的問題,我試圖找出如何克服它。那裏的答案沒有解決。 – JGlass

回答

0

我認爲問題來自於生成的映射查找子元素,但不查找文本。

如果你可以修改您的XSD,解決辦法是:

<xsd:complexType name="AttributeType"> 
    <xsd:complexContent mixed="true"> 
     <xsd:extension base="xsd:anyType"> 
     <xsd:attribute name="id" type="xsd:anyURI" use="required"/> 
     <xsd:anyAttribute processContents="lax"/> 
     </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 

但因爲你不能......

如果你能負擔得起修改源代碼,更改:

@XmlAnyElement 
protected List<Element> any; 

@XmlAnyElement 
@XmlMixed 
protected List<Object> any; 

對象列表應包含子元素的Element和文本的String

+0

更改生成的源代碼在這裏也不是最好的解決方案,因爲XSD在生成過程中「經常」更新並生成。有沒有辦法通過綁定自定義來完成這種修改? – Ingo