首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用org.eclipse.xsd.util.XSDResourceImpl读取XSD

使用org.eclipse.xsd.util.XSDResourceImpl读取XSD
EN

Stack Overflow用户
提问于 2016-02-05 11:03:01
回答 3查看 282关注 0票数 5

我使用org.eclipse.xsd.util.XSDResourceImpl成功地读取了XSD模式,并处理了所有包含的XSD元素、类型、属性等。

但是,当我想处理对导入模式中声明的元素的引用时,我将null作为它的类型。导入的模式似乎不是由XSDResourceImpl处理的。有什么想法吗?

代码语言:javascript
运行
复制
    final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath));
    rsrc.load(new HashMap());
    final XSDSchema schema = rsrc.getSchema();
    ...
    if (elem.isElementDeclarationReference()){ //element ref
        elem = elem.getResolvedElementDeclaration();
    }
    XSDTypeDefinition tdef = elem.getType(); //null for element ref

更新:

我使导入的XSD无效,但也没有例外。这意味着它真的没有被解析。有没有办法强制加载进口的XSD和主要的XSD?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-23 12:36:48

自动处理importsincludes有一个重要的技巧。您必须使用ResourceSet来读取主XSD文件。

代码语言:javascript
运行
复制
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.eclipse.xsd.XSDSchema;

static ResourceSet resourceSet;
XSDResourceFactoryImpl rf = new XSDResourceFactoryImpl();
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", rf);
resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_LOCATION, Boolean.TRUE);
XSDResourceImpl rsrc = (XSDResourceImpl)(resourceSet.getResource(uri, true));
XSDSchema sch = rsrc.getSchema();

然后,在处理元素、属性或模型组之前,您必须使用以下命令:

代码语言:javascript
运行
复制
elem = elem.getResolvedElementDeclaration();
attr = attr.getResolvedAttributeDeclaration();
grpdef = grpdef.getResolvedModelGroupDefinition();
票数 1
EN

Stack Overflow用户

发布于 2016-02-10 12:37:32

您是否可以尝试这样的方法,手动解析类型:

代码语言:javascript
运行
复制
final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath));
rsrc.load(new HashMap());
final XSDSchema schema = rsrc.getSchema();
for (Object content : schema.getContents())
{
    if (content instanceof XSDImport)
    {
        XSDImport xsdImport = (XSDImport) content;
        xsdImport.resolveTypeDefinition(xsdImport.getNamespace(), "");
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-02-12 12:15:33

你可以看看这里。这种方法的特别之处是:

代码语言:javascript
运行
复制
   private static void forceImport(XSDSchemaImpl schema) { 
        if (schema != null) { 
            for (XSDSchemaContent content: schema.getContents()) { 
                if (content instanceof XSDImportImpl) { 
                    XSDImportImpl importDirective = (XSDImportImpl)content; 
                    schema.resolveSchema(importDirective.getNamespace()); 
                } 
            } 
        } 
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35222522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档