Find items that can be repeated in an xml schema using Java

亡梦爱人 提交于 2019-12-23 05:04:05

问题


I want to deduce from an XML schema the couple (parentTag, childTag) such that it is allowed for parentTag to contain multiple instances of childTag as direct children.

Doing that by hand, I look for the maxOccurs attribute in the schema, look the element tag, and tag of the direct parent.

For example, from

<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">

I should obtain the couples (A,B) and (ANOTHER,B).

I have a working solution using XSLT to convert my schema to a list of such (parentTag, childTag) couples.

Is there an elegant way to do this in Java ? Which library would you recommend to implement this ?


回答1:


To process XML schemas in Java (without XSLT), we use Xerces2 Java Parser: http://xerces.apache.org/xerces2-j/

The following packages/classes may be needed:

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;

Then, the processing of an XSD file goes like this:

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

...


来源:https://stackoverflow.com/questions/17471066/find-items-that-can-be-repeated-in-an-xml-schema-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!