【发布时间】:2011-11-09 17:56:14
【问题描述】:
在浏览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>
我希望阅读所有元素,而不仅仅是基本元素,但不知道如何继续。感谢您的帮助。
【问题讨论】:
-
用户按标签而不是标题浏览。在标题中重复主标签
[java]是不必要的,它只会使整个list of Java questions 混乱。