java - 从 XMI 文件加载 EMF 模型实例

标签 java model emf xmi ecore

我知道有几个关于这个主题的 QnA。我尝试了很多解决方案,但总是遇到同样的错误。

我的代码结构是这样的:

Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("xmi", new XMIResourceFactoryImpl());

    ResourceSet resSet = new ResourceSetImpl();
    Resource resource = resSet.getResource(URI.createURI("model/List.xmi"), true);
    resource.load(Collections.EMPTY_MAP);
    EObject root = resource.getContents().get(0);

错误:

Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'List' not found. (file:///C:/Users/2/My%20Repository/UNIT%20Research%20and%20Development/com.unitbilisim.research.transformation/model/List.xmi, 6, 40) at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319) at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278) at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406) at com.unitbilisim.research.transformation.ConvertEcore2Graph.main(ConvertEcore2Graph.java:61) Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'List' not found. (file:///C:/Users/2/My%20Repository/UNIT%20Research%20and%20Development/com.unitbilisim.research.transformation/model/List.xmi, 6, 40) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2625) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2458) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1335) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1504) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1026) at org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMIHandler.java:77) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:1008) at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:719) at org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHandler.java:163) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175) at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:261) at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518) at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297) at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259) at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274) ... 2 more

“未找到带有 uri 的包”是什么意思?我可以直接读取 xmi 文件还是需要将其解析为 xml 文件?

我也试过这个 -> https://stackoverflow.com/a/4615965/1604503

    XMIResource resource = new XMIResourceImpl(URI.createURI("model/List.xmi"));
    resource.load(null);
    System.out.println( resource.getContents().get(0) );

PackageNotFoundEx。和 Reource$IOWrappedEx。再次:(

请帮忙

亲切的问候

最佳答案

原因是,我没有生成模型代码。因此,无法找到“列表”包。我确实将它添加到我的 packageRegistry 中,仅此而已。

        ResourceSet resourceSet = new ResourceSetImpl();

        // register UML
        Map packageRegistry = resourceSet.getPackageRegistry();
        packageRegistry.put(list.ListPackage.eNS_URI, list.ListPackage.eINSTANCE);

        // Register XML resource as UMLResource.Factory.Instance
        Map extensionFactoryMap = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
        extensionFactoryMap.put("xmi", new XMIResourceFactoryImpl());

        Resource resource = (Resource) resourceSet.createResource(uri);


        // try to load the file into resource
        resource.load(null);

关于java - 从 XMI 文件加载 EMF 模型实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26944174/

相关文章:

java - Hadoop-级联: Partial directory source tap

model - 需要一些帮助来理解模型和数据提供者

java - 在 EMF Compare 计算的 ADD Diff 中检索 "match of value"

java - C3P0 max语句配置?

java - 评估异常: The method 'start' is not supported by this command processor

java - 从线程获取资源

java - 在 EMF 模型中使用 Java 关键字 volatile

java - OpenNLP 解析器训练

c# - MVC 3 多表单模型传递给字典

java - 在 Eclipse 上设置 EMF Compare 并编译