提取元素文本值

2024-04-17

我在用JAXB使用以下命令生成将 xml 解组为 java 实体的代码xsd原理图文件。问题是生成的代码不会生成name of an organization在以下 xml 中指定:

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

这是 xsd 的定义Organization数据类型:

<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>

这里是xsd的定义ON数据类型:

<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>

这是由以下命令创建的结果java代码JAXB:

@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;而不是创建单独的属性delimiter, prefix, and suffix。同样重要的是,它也无法让我访问name顶部 xml 中的标签。当我删除mixed="true"来自ONxsd 文件中的定义,内容列表被替换为单独的属性delimiter, prefix, and suffix,但我仍然无法获取文本内容name元素。我犹豫要不要删除mixed=true因为我读到 mix=true 指定了complextype可以包含elements, attributes, and text.

我如何更改上面的代码,以便生成一个用于检索文本的方法name元素除了为每个其他元素/属性生成单独的方法之外?


Try my 简化插件 http://confluence.highsource.org/display/J2B/Simplify+Plugin。我不太确定它是否符合您的要求,但它是为类似的情况编写的。

Example:

<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;

但是使用简化插件:

<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,请向我发送带有测试用例的 PRhere https://github.com/highsource/jaxb2-basics/blob/master/tests/issues/src/main/resources/schema.xsd并提出问题here https://github.com/highsource/jaxb2-basics/issues.

UPDATE

我已经实施了此功能 https://github.com/highsource/jaxb2-basics/issues/1.

From this https://github.com/highsource/jaxb2-basics/blob/master/tests/issues/src/main/resources/schema.xsd#L335-L346:

<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)中出现。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

提取元素文本值 的相关文章

随机推荐