4

使用这个简化的 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 文件(使用<xmlbean>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 并且从未遇到过这个问题,这就是为什么我确定我丢失了一些东西但我一直找不到它的原因。

4

2 回答 2

3

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

于 2011-04-28T19:27:42.560 回答
0

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

于 2014-08-25T10:01:52.433 回答