提取元素文本值 [英] extracting element text value

查看:88
本文介绍了提取元素文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JAXB 来生成用于将xml解组到java实体中的代码,使用 xsd 文件进行原理图。问题是生成的代码不会生成以下xml中指定的组织名称

I am using JAXB to generate code for unmarshalling xml into java entities using an xsd file for schematics. The problem is that the resulting code does not generate the name of an organization specified in the following xml:

<organization>
    <name>Some organization's name goes here</name>
</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>

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

这是由 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; 而不是为分隔符前缀和<$ c $创建单独的属性C>后缀。同样重要的是,它也无法让我访问顶部xml中 name 标记内的文本值。当我从xsd文件中的 ON 定义中删除 mixed =true时,内容列表将替换为分隔符前缀后缀的单独属性,但我仍然无法获得 name 元素的文本内容。我犹豫是否删除 mixed = true 因为我读到mixed = true指定 complextype 可以包含元素属性文本

There are several problems with this resulting java class. First off, it creates List<Serializable> content; instead of creating separate properties for delimiter, prefix, and suffix. And just as importantly, it also fails to give me access to the text value inside the name tags in the xml at top. When I remove the mixed="true" from the ON definition in the xsd file, the content list is replaced with separate properties for the delimiter, prefix, and suffix, but I still can't get the text content of the name element. I hesitate to remove mixed=true because I read that mixed=true specified that a complextype can contain elements, attributes, and text.

如何更改上面的代码,以便生成一个方法来检索 name 元素的文本,同时为每个元素生成单独的方法其他元素/属性?

How can I change the code above so that is generates a method for retrieving the text of the name element in addition to generating separate methods for each of the other elements/properties?

推荐答案

试试我的简化插件。我不确定它是否符合您的要求,但它是针对类似情况编写的。

Try my Simplify plugin. I am not exactly sure that it does what you want, but it is written for the similar case.

示例:

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

你有一个类似的情况,你得到你的财产因为混合=true

You have a somewhat similar case, you get your property because of the mixed="true".

如果这对混合类型OOTB不起作用,请将测试用例此处并提出问题< a href =https://github.com/highsource/jaxb2-basics/issues =nofollow>这里。

If this does not work for the mixed types OOTB, please send me a PR with your test case here and file an issue here.

更新

我已实施此功能

来自 this

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

Will be in the next version (0.9.0).

这篇关于提取元素文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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