解析 XSD 并获取嵌套元素 [英] Parsing XSD and getting nested elements
问题描述
经过这个页面后,我写了以下代码解析 XSD 文件.但是我只得到根元素,我迷失了如何在其中获取嵌套元素.代码:
After going through this page, I wrote the following code to parse an XSD file. However I only get the root element, and I am lost so as to how to get the nested elements inside it. Code:
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,但这是结构:
I am not posting the entire xsd, but this is the structure:
<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>
我希望阅读所有元素,而不仅仅是基本元素,但不确定如何继续.感谢您的帮助.
I am looking to read all the elements, not just the base element, and not sure how to proceed. Thanks for the help.
推荐答案
我认为您试图迭代 XSNamedMap
错误.您只获得 country 元素的原因是因为它是根元素.您可能必须深入到 XSNamespaceItem
并调用 getComponents
检索另一组 XSNamedMap
对象.
I think your trying to iterate over the XSNamedMap
incorrectly. The reason you are getting only the country element is because it is the root element. You will probably have to descend down into XSNamespaceItem
and call getComponents
to retrieve another set of XSNamedMap
objects.
这将正确解析 XSD,但您仍然需要遍历树.
This will properly parse the XSD but you still have to traverse the tree.
这篇关于解析 XSD 并获取嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!