使用Java查找可以在xml模式中重复的项

使用Java查找可以在xml模式中重复的项,java,xml,xsd,Java,Xml,Xsd,我想从XML模式中推断出这对夫妇(parentTag,childTag),这样它就允许parentTag包含多个childTag实例作为直接子对象 通过手工操作,我在模式中查找maxOccurs属性,查找元素标记和直接父级的标记 例如,从 <xs:complexType name="aType"> <xs:sequence> <xs:element ref="B" maxOccurs="unbounded"/> <

我想从XML模式中推断出这对夫妇
(parentTag,childTag)
,这样它就允许
parentTag
包含多个
childTag
实例作为直接子对象

通过手工操作,我在模式中查找
maxOccurs
属性,查找元素标记和直接父级的标记

例如,从

<xs:complexType name="aType">
     <xs:sequence>
          <xs:element ref="B" maxOccurs="unbounded"/>
     </xs:sequence>
</xs:complexType>

<xs:element name="A" type="aType">
<xs:element name="ANOTHER" type="aType">

我应该获得夫妇
(A,B)
(另一个,B)

我有一个有效的解决方案,使用XSLT将我的模式转换为这样的
(parentTag,childTag)
对的列表


在Java中有没有一种优雅的方法可以做到这一点?您建议使用哪个库来实现此功能?

要在Java中处理XML模式(不使用XSLT),我们使用Xerces2 Java解析器:

可能需要以下包/类:

import org.w3c.dom.*;
import org.apache.xerces.xs.*;
import org.apache.xerces.dom.DOMXSImplementationSourceImpl;
import org.apache.xerces.impl.xs.util.StringListImpl;
import org.apache.xerces.util.XMLCatalogResolver;
然后,XSD文件的处理如下所示:

// Obtain the XML Schema implementation
XSImplementation impl = (XSImplementation)
  (new DOMXSImplementationSourceImpl()).getDOMImplementation(XMLConstants.XSD_LOADER_NAME);

// Get schema loader
XSLoader schemaLoader = impl.createXSLoader (null);

// Optional. Specify error handler
DOMErrorHandler errorHandler = ....;
DOMConfiguration config = schemaLoader.getConfig();
config.setParameter("error-handler", errorHandler);

// Optional. Specify XML catalog resolver.
// This may be needed to redirect internal DTD/schema file references
XMLCatalogResolver catalogResolver = ...;
config.setParameter("resource-resolver", catalogResolver);

String xsdURI = ...; // the location of schema file

// read schema
XSModel xsModel = schemaLoader.loadURI(xsdURI);

// PROCESS SCHEMA (here, you can do anything you want)

XSNamedMap xsMap;

// process top-level element declarations
xsMap = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);
for (int i = 0; i < xsMap.getLength(); i ++)
{
  XSElementDeclaration xsElementDecl = (XSElementDeclaration) xsMap.item(i);
  ...
}

// process top-level type definitions
xsMap = xsModel.getComponents(XSConstants.TYPE_DEFINITION);
for (int i = 0; i < xsMap.getLength(); i ++)
{
  XSTypeDefinition xsTDef = (XSTypeDefinition) xsMap.item(i);
  ...
}

// process model group definitions
xsMap = xsModel.getComponents(XSConstants.MODEL_GROUP_DEFINITION);
for (int i = 0; i < xsMap.getLength(); i ++)
{
  XSModelGroupDefinition xsGroupDef = (XSModelGroupDefinition) xsMap.item(i);
  ...
}

...
//获取XML架构实现
xsimpl=(XSImplementation)
(新的DOMXSImplementationSourceImpl()).getDOMImplementation(xmlstants.XSD_LOADER_NAME);
//获取模式加载器
XSLoader schemaLoader=impl.createXSLoader(null);
//可选。指定错误处理程序
DoErrorHandler errorHandler=。。。。;
DOMConfiguration config=schemaLoader.getConfig();
setParameter(“错误处理程序”,errorHandler);
//可选。指定XML目录解析器。
//这可能需要重定向内部DTD/schema文件引用
XMLCatalogResolver catalogResolver=。。。;
config.setParameter(“资源解析器”,catalogResolver);
字符串xsdURI=…;//架构文件的位置
//读取模式
XSModel=schemaLoader.loadURI(xsdURI);
//流程模式(在这里,您可以做任何您想做的事情)
XSNamedMap xsMap;
//处理顶级元素声明
xsMap=xsModel.getComponents(XSConstants.ELEMENT_声明);
对于(int i=0;i
Hello,根据您的评论,我正在尝试使用
XERCES2
,但在解析过程中,我无法找到ComplexType`中的所有元素。我在这里发布了一个问题:如果你有机会,请你看一看,如果可能的话,提供解决方案。非常感谢:)