提取元素文本值

时间:2014-09-29 19:49:27

标签: java xml jaxb xsd

我正在使用JAXB生成用于将xml解组为java实体的代码,使用xsd文件进行原理图。问题是生成的代码不会生成以下xml中指定的name的{​​{1}}:

organization

以下是<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

这个生成的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; } } ,而不是为List<Serializable> content;delimiterprefix创建单独的属性。同样重要的是,它也无法让我访问顶部xml中suffix标记内的文本值。当我从xsd文件中的name定义中删除mixed="true"时,内容列表将替换为ONdelimiterprefix的单独属性,但我仍然无法获得suffix元素的文本内容。我不愿意删除name,因为我读到mixed = true指定mixed=true可以包含complextypeelementsattributes

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

1 个答案:

答案 0 :(得分:0)

试试我的Simplify plugin。我并不完全确定它能做到你想要的,但它是针对类似的情况编写的。

示例:

<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,请向我发送包含测试用例here的PR并提交问题here

<强>更新

我已实施this feature

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