2017-03-15 59 views
0

我使用Apache Xerces來解析以下XSD文件。Apache Xerces獲取xsd中每個元素的聲明

<xs:element name="Root" type="RootType"> 
    ... 
</xs:element> 

<xs:complexType name="RootType"> 
    <xs:complexContent> 
     <xs:extension base="BaseElement"> 
      <xs:choice maxOccurs="unbounded"> 
       <xs:element name="ElementA" type="ElementAType" /> 
       <xs:element name="ElementB" type="ElementBType" /> 
       <xs:element name="ElementC" type="ElementCType" /> 
      </xs:choice> 

      <xs:attribute ... >...</xs:attribute> 
      <xs:attribute ... >...</xs:attribute> 
      <xs:attribute ... >...</xs:attribute> 
     </xs:extension> 
    </xs:complexContent> 
</xs:complexType> 

<xs:complexType name="BaseElement"> 
    ... 
</xs:complexType> 

<xs:complexType name="ElementAType"> 
    ... 
</xs:complexType> 

<xs:complexType name="ElementBType"> 
    ... 
</xs:complexType> 

<xs:complexType name="ElementCType"> 
    ... 
</xs:complexType> 

我想獲取文件中每個元素的聲明,即:Root,ElementA,ElementB和ElementC。方法:

XSElementDeclaration decl = model.getElementDeclaration("ElementA"); 

爲ElementA,ElementB和ElementC返回null。它只找到根元素的聲明。

XSNamedMap comp = model.getComponents(XSConstants.ELEMENT_DECLARATION); 

也不起作用,它只返回Root聲明。 如何讓這些聲明嵌套在RootType中?

回答

1

您必須遞歸搜索模式組件模型。從全局元素聲明(XSElementDeclaration)getTypeDefinition()可以讓您進入XSComplexTypeDefinition。從那裏遞歸地使用getParticle()和getTerm()將最終讓你到達表示本地元素聲明的XSElementDeclaration對象。

(撒克遜SCM文件主要包含相同的數據結構,但在一個XML格式,它可以讓你更方便的搜索,使用XPath表達式,而不是一步一步程序導航。)

+0

謝謝,現在我得到它!但是,我遇到了另一個問題(我編輯了我的問題)。 –

+0

請不要在獲得答案後更改問題,這會使線程非常難以遵循。如果您有其他問題,請在新線索中提出。 –

+0

好吧,我已經撤消了更改。 –