使用這種簡化的XSD(簡化,但仍冗長,因爲所有的XSD往往是):消氣上的XMLBeans生成的類返回null,它不應該
<?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>
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之前並沒有遇到這個問題,這就是爲什麼我確定我錯過了一些東西,但是我一直無法找到它。
這幾乎讓我需要去的地方。更大的實際問題隨後出現了一些XML命名空間問題,但是這讓我經歷了那些空白的孩子,謝謝! – 2011-04-29 15:07:02