【发布时间】:2021-06-25 15:23:08
【问题描述】:
我正在使用Apache XmlSchema Core 库来解析XSD 文件并获取所有元素及其子类型(数据类型、maxOccurs 等)。我正在关注文档Apache XML SCHEMA CORE 并尝试这样做。但是在导航到某个点之后,我有点困惑。有人可以指导我如何遍历我的XSD 文件并获取所有元素及其子元素以及相关信息吗?
我能够在我的schema 元素中获取所有 XSD 信息我只想知道如何从我的根 RootFood 访问子元素并获取其相关信息。任何帮助将不胜感激。
我试图继续前进,这就是我目前所拥有的:
元素RootFood 属于类XmlSchemaGroupParticle 的实例。我试图调试代码以查找与我的rootParticles 关联的元素,它有一个名为items 的字段,其中我有我的food 元素items->[0]->namedDelegate->qName->localPart 但是当我尝试添加GET 方法时rootParticles获取items则没有这种方法。
XmlSchemaParticle 扩展了以下类:XmlSchemaAnnotated、XmlSchemaObject,并实现了接口XmlSchemaObjectBase,但它们都没有名为Items 的字段。
以下是我到目前为止的Java代码,我尝试了几件事:
public class XMLSchemaCore {
public static void main(String[] args) throws URISyntaxException,
FileNotFoundException,
UnsupportedEncodingException {
String xsdPath = Paths.get(XMLSchemaCore.class.getClassLoader().getResource("test.xsd").toURI()).toFile().getAbsolutePath();
String filePath = Path.of(xsdPath).toString();
InputStream is = new FileInputStream(filePath);
XmlSchemaCollection schemaCol = new XmlSchemaCollection();
// Schema contain the complete XSD content which needs to be parsed
XmlSchema schema = schemaCol.read(new StreamSource(is));
// schema.write(System.out);
for (Map.Entry<QName, XmlSchemaElement> entry: schema.getElements().entrySet()) {
// Root element and its contents
QName parentElementName = entry.getKey();
XmlSchemaElement parentElementValues = entry.getValue();
// Get the elements based on the Root element
XmlSchemaElement root = schema.getElementByName(parentElementName);
// Get the type of the root element
XmlSchemaType type = root != null ? root.getSchemaType() : null;
// Check if the root element is of Complex type
if (type instanceof XmlSchemaComplexType) {
// Get the Particles associated with the element
XmlSchemaParticle rootParticles = ((XmlSchemaComplexType) type).getParticle();
// Check particle belongs to which type
if (rootParticles instanceof XmlSchemaAny) {
System.out.println("Any Schema Type");
} else if (rootParticles instanceof XmlSchemaElement) {
System.out.println("Element Schema Type");
} else if (rootParticles instanceof XmlSchemaGroupParticle) {
System.out.println("Group Schema Type");
System.out.println(rootParticles);
System.out.println(rootParticles);
} else if (rootParticles instanceof XmlSchemaGroupRef) {
System.out.println("Group Ref Schema Type");
}
}
}
}
}
以下是我拥有的XSD 文件:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RootFood">
<xs:complexType>
<xs:sequence>
<xs:element name="food">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" />
<xs:element type="xs:string" name="price" />
<xs:element type="xs:string" name="description" />
<xs:element type="xs:short" name="calories" />
<xs:element name="ingredients">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ingredient"
maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
【问题讨论】:
-
终于,经过多次尝试,我能够做到。请在下面找到代码。