【问题标题】:Getter on xmlbeans generated class returning null and it shouldn'txmlbeans 上的 getter 生成的类返回 null 并且它不应该
【发布时间】:2011-08-14 23:32:38
【问题描述】:

使用这个简化的 XSD(简化,但仍然像所有 XSD 一样冗长):

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="[redacted]">
 <xsd:element name="Statement" type="BILLINGSTATEMENTTYPEType"/>

 <xsd:complexType name="BILLINGSTATEMENTTYPEType">
  <xsd:sequence>
   <xsd:element name="AccountSection" type="ACCOUNTSECTIONTYPEType"/>
   <xsd:element name="DataSection" type="DATASECTIONTYPEType"/>
   <xsd:element name="Summary" type="SUMMARYTYPEType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="ACCOUNTSECTIONTYPEType">
  <xsd:sequence>
    <xsd:element name="Foo" type="xsd:string" maxOccurs="unbounded" minOccurs="0"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="DATASECTIONTYPEType">
  <xsd:sequence>
   <xsd:element name="Bar" type="xsd:string" maxOccurs="unbounded" minOccurs="0"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="SUMMARYTYPEType">
  <xsd:sequence>
   <xsd:element name="Baz" type="xsd:string" maxOccurs="unbounded" minOccurs="0"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

我生成了一个 JAR 文件(使用 &lt;xmlbean&gt; Ant task from xmlbeans),一切看起来都很棒,我得到了所有正确的类型等等。但是当我让它解析这个简化的文档时:

<Statement>
    <AccountSection>
        <Foo>bar</Foo>
    </AccountSection>
    <DataSection>
    </DataSection>
    <Summary>
    </Summary>
</Statement>

使用此代码:

public class XmlTest {
    public static void main(String[] args) throws Exception {
        File xmlFile = new File("./data/test.xml");
        FileInputStream xmlStream = new FileInputStream(xmlFile);

        BILLINGSTATEMENTTYPEType statement = BILLINGSTATEMENTTYPEType.Factory.parse(xmlStream);

        ACCOUNTSECTIONTYPEType acctSection = statement.getAccountSection();

        System.out.println(statement.xmlText());
        System.out.println("acctSection is null:" + (acctSection == null));
    }
}

acctSection(以及我尝试过的任何子部分)始终为空,即使它正在完全解析文档。

输出:

<Statement>
    <AccountSection>
        <Foo>bar</Foo>
    </AccountSection>
    <DataSection>
    </DataSection>
    <Summary>
    </Summary>
</Statement>
acctSection is null:true

为什么它是空的?为什么它们都是空的?我是否在 XSD 中的某个地方不正确地定义了某些东西?我之前成功使用过 xmlbeans 并且从未遇到过这个问题,这就是为什么我确定我丢失了一些东西但我一直找不到它的原因。

【问题讨论】:

    标签: java xml parsing xmlbeans


    【解决方案1】:

    我自己不是 xmlbeans 中的导出,但我注意到您使用了复杂类型的 Factory 来解析 xml。您可以尝试改用StatementDocument.Factory 吗?

    【讨论】:

    • 这几乎把我带到了我需要去的地方。更大的实际问题随后出现了一些 XML 命名空间问题,但这让我摆脱了那些 null 孩子,谢谢!
    【解决方案2】:

    通过在我的 .xsd 文件的命名空间中添加 elementFormDefault="qualified" 解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2011-10-31
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多