【问题标题】:Find items that can be repeated in an xml schema using Java使用 Java 查找可以在 xml 模式中重复的项目
【发布时间】:2013-07-02 12:07:24
【问题描述】:

我想从 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)(ANOTHER,B)这对夫妇。

我有一个可行的解决方案,使用 XSLT 将我的架构转换为此类 (parentTag, childTag) 夫妇的列表。

在 Java 中是否有一种优雅的方法可以做到这一点?你会推荐哪个库来实现这个?

【问题讨论】:

    标签: java xml xsd


    【解决方案1】:

    为了在 Java 中处理 XML 模式(没有 XSLT),我们使用 Xerces2 Java Parser:http://xerces.apache.org/xerces2-j/

    可能需要以下包/类:

    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);
      ...
    }
    
    ...
    

    【讨论】:

    • 您好,我正在尝试根据您的评论使用XERCES2,但在解析过程中我无法找到 ComplexType` 中的所有元素。我已经在这里发布了这个问题:stackoverflow.com/questions/66856898/… 如果你有机会,请你看看它并尽可能提供解决方案。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2012-02-16
    • 2015-11-14
    • 2014-05-16
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多