xmlbeans 生成的类上的 getter 返回 null,但它不应该

发布于 2024-11-03 22:28:31 字数 2820 浏览 1 评论 0原文

使用这个简化的 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 文件(使用 来自 xmlbeans 的 Ant 任务),一切看起来都很棒,我得到了所有正确的类型等等。但是当我让它解析这个简化的文档时:

<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,但从未遇到过此问题,这就是为什么我确信我丢失了某些内容,但我一直无法找到它。

Using this simplified XSD (simplified, but still verbose as all XSDs tend to be):

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

I generated a JAR file (using the <xmlbean> Ant task from xmlbeans), and everything appears to look great, I get all the right types and whatnot. But when I have it parse this simplified document:

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

Using this code:

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

The acctSection (and any of the child sections I've tried) are always null, even though it is fully parsing the document.

Output:

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

Why is it null? Why are they all null? Did I improperly define something somewhere in my XSD? I've used xmlbeans before successfully and never had this issue, which is why I'm sure I'm missing something but I've been unable to find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅哥哥的热头脑 2024-11-10 22:28:31

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

I'm not an export in xmlbeans myself, but I noticed that you used the Factory of the complex type to parse the xml. Can you try to use StatementDocument.Factory instead?

独留℉清风醉 2024-11-10 22:28:31

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

My problem was solved by adding elementFormDefault="qualified" in the namespace of my .xsd file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文