Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 提取元素文本值_Java_Xml_Jaxb_Xsd - Fatal编程技术网

Java 提取元素文本值

Java 提取元素文本值,java,xml,jaxb,xsd,Java,Xml,Jaxb,Xsd,我正在使用JAXB生成代码,以便使用xsd文件将xml解组为java实体以用于原理图。问题在于,生成的代码不会生成以下xml中指定的组织名称: <organization> <name>Some organization's name goes here</name> </organization> 以下是ON数据类型的xsd定义: <xs:complexType name="ON" mixed="true"> <

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

<organization>
    <name>Some organization's name goes here</name>
</organization>
以下是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>
由此产生的java类有几个问题。首先,它创建列表内容;而不是为分隔符、前缀和后缀创建单独的属性。同样重要的是,它也无法让我访问顶部xml中名称标记内的文本值。当我从xsd文件中的ON定义中删除mixed=true时,内容列表将替换为分隔符、前缀和后缀的单独属性,但我仍然无法获取name元素的文本内容。我不太愿意删除mixed=true,因为我读到mixed=true指定complextype可以包含元素、属性和文本

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

试试我的方法。我不确定它是否符合您的要求,但它是为类似的情况编写的

例如:

<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>
您将得到以下结果:

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

将出现在下一个版本0.9.0中。

您没有回答我的问题。我试图在不丢失属性或子节点的情况下获取name元素的文本内容。我并不想批评你,但你似乎只是在没有真正考虑我的问题的情况下为你的网站发布了一则广告。首先,它创建列表内容;而不是为分隔符、前缀和后缀创建单独的属性。这是你问题的一部分,不是吗?我宣传的插件正是针对这一点。您在最后一行询问如何分离属性,所讨论的插件正是这样做的。你还认为我的回答不恰当吗?元素的文本内容应该在您获得的List属性中,毕竟字符串是可序列化的。我知道我可以通过去掉xsd中的mixed属性来去掉XmlElementRefs。但是这样做会使我无法访问name元素的文本组件。我的问题是,在通过删除mixed除去xmlementrefs之后,如何访问name元素的文本组件。我不明白oracle的架构师怎么会忽视这一点,以至于需要添加第三方库。上面代码生成的ON类没有getContent方法,即使我删除了定义JAXB类参数的xsd的mixed属性,getContent也应该是元素文本内容的字符串。
<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;
<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;