在浏览this页面后,我编写了以下代码来解析XSD文件。但是我只获得了根元素,并且我迷失了如何获取其中的嵌套元素。 代码:
XMLSchemaLoader loader = new XMLSchemaLoader();
XSModel model = loader.loadURI(url.toURI().toString());
XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION); //returns the root component
if(map!=null ){
for (int j=0; j<map.getLength(); j++) {
String name = map.item(j).getName(); //returns 'country' correctly.
}
}
我没有发布整个xsd,但这是结构:
<xsd:element name="country">
<xsd:complexType>
<xsd:annotation>
<xsd:appinfo id="substring">No</xsd:appinfo>
</xsd:annotation>
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="states" minOccurs="1" maxOccurs="1" >
<xsd:complexType>
<xsd:annotation>
<xsd:appinfo id="substring">No</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="cities" minOccurs="1" maxOccurs="unbounded">
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>
我希望阅读所有元素,而不仅仅是基本元素,并且不确定如何继续。谢谢你的帮助。
答案 0 :(得分:1)
我认为你试图错误地迭代XSNamedMap
。您只获得country元素的原因是因为它是根元素。您可能需要下降到XSNamespaceItem
并调用getComponents
来检索另一组XSNamedMap
个对象。
这将正确解析XSD,但您仍然必须遍历树。