簡體   English   中英

提取元素文本值

[英]extracting element text value

我正在使用JAXB生成代碼,以使用用於原理圖的xsd文件將xml編組為Java實體。 問題在於結果代碼不會生成以下xml中指定的organizationname

<organization>
    <name>Some organization's name goes here</name>
</organization>

這是Organization數據類型的xsd定義:

<xs:complexType name="Organization">
    <xs:sequence>
        <xs:element name="name" type="ON" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="classCode" type="EntityClassOrganization" use="optional" fixed="ORG"/>
</xs:complexType>

這是ON數據類型的xsd定義:

<xs:complexType name="ON" mixed="true">
    <xs:annotation>
      <xs:documentation>
        A name for an organization. A sequence of name parts.
     </xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="delimiter" type="en.delimiter"/>
        <xs:element name="prefix" type="en.prefix"/>
        <xs:element name="suffix" type="en.suffix"/>
    </xs:sequence>
    <xs:attribute name="use" use="optional" type="set_EntityNameUse">
      <xs:annotation>
        <xs:documentation>
            A set of codes advising a system or user which name
            in a set of like names to select for a given purpose.
            A name without specific use code might be a default
            name useful for any purpose, but a name with a specific
            use code would be preferred for that respective purpose.
        </xs:documentation>
      </xs:annotation>
    </xs:attribute>
</xs:complexType>

這是JAXB創建的結果Java代碼:

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

    @XmlElementRefs({
        @XmlElementRef(name = "delimiter", namespace = "urn:something", type = JAXBElement.class),
        @XmlElementRef(name = "prefix", namespace = "urn:something", type = JAXBElement.class),
        @XmlElementRef(name = "suffix", namespace = "urn:something", type = JAXBElement.class)
    })
    @XmlMixed
    protected List<Serializable> content;
    @XmlAttribute(name = "use")
    protected List<String> use;

    public List<Serializable> getContent() {
        if (content == null) {content = new ArrayList<Serializable>();}
        return this.content;
    }

    public List<String> getUse() {
        if (use == null) {use = new ArrayList<String>();}
        return this.use;
    }
}  

這個生成的java類存在幾個問題。 首先,它創建List<Serializable> content; 而不是為delimiterprefixsuffix創建單獨的屬性。 同樣重要的是,它也無法使我訪問頂部xml中name標簽內的文本值。 當我從xsd文件中的ON定義中刪除mixed="true" ,內容列表被delimiterprefixsuffix單獨屬性替換,但是我仍然無法獲取name元素的文本內容。 我猶豫刪除了mixed=true因為我讀到mixed = true指明了complextype可以包含elementsattributestext

我如何更改上面的代碼,以便除了為每個其他元素/屬性生成單獨的方法之外,還生成一種用於檢索name元素的文本的方法?

試試我的Simplify插件 我不確定它是否可以滿足您的要求,但是它是針對類似情況編寫的。

例:

<xs:complexType name="typeWithElementsProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="a" type="xs:string"/>
        <xs:element name="b" type="xs:int"/>
    </xs:choice>
</xs:complexType>

給你:

@XmlElements({
    @XmlElement(name = "a", type = String.class)
    @XmlElement(name = "b", type = Integer.class),
})
protected List<Serializable> aOrB;

但是使用了simple插件:

<xs:complexType name="typeWithElementsProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:annotation>
            <xs:appinfo>
                <simplify:as-element-property/>
            </xs:appinfo>
        </xs:annotation>
        <xs:element name="a" type="xs:string"/>
        <xs:element name="b" type="xs:int"/>
    </xs:choice>
</xs:complexType>

你會得到:

@XmlElement(name = "a", type = String.class)
protected List<String> a;
@XmlElement(name = "b", type = Integer.class)
protected List<Integer> b;

您有一個類似的案例,您的財產是因為mixed="true"

如果這不適用於混合類型的OOTB,請在此處向我發送帶有測試用例的PR,並在此處提出問題。

更新

我已經實現了此功能

這個

<xs:complexType name="gh1" mixed="true">
    <xs:sequence>
        <xs:element name="a" type="xs:string">
            <xs:annotation>
            <xs:appinfo>
                <simplify:as-element-property/>
            </xs:appinfo>
        </xs:annotation>
        </xs:element>
        <xs:element name="b" type="xs:int"/>
    </xs:sequence> 
</xs:complexType>

您會得到:

protected List<String> a;
@XmlElement(type = Integer.class)
protected List<Integer> b;
@XmlMixed
protected List<String> content;

將在下一版本(0.9.0)中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM