简体   繁体   English

从XMI文件加载EMF模型实例

[英]Load EMF model instance from XMI file

I know there are several QnA about this subject. 我知道有几个有关此主题的QnA。 I have tried lots of solution, I am always getting same error. 我尝试了很多解决方案,但总是遇到相同的错误。

My code structure is like: 我的代码结构如下:

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

Error: 错误:

Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'List' not found. 线程“主”中的异常org.eclipse.emf.ecore.resource.impl.ResourceSetImpl $ 1DiagnosticWrappedException:org.eclipse.emf.ecore.xmi.PackageNotFoundException:未找到带有uri“列表”的软件包。 (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. (文件:/// C:/Users/2/My%20Repository/UNIT%20Research%20and%20Development/com.unitbilisim.research.transformation/model/List.xmi,6,40),网址为org.eclipse.emf。 org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)上的ecore.resource.impl.ReleaseSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)在org.eclipse.emf.ecore.resource处。 com.unitbilisim.research.transformation.ConvertEcore2Graph.main(ConvertEcore2Graph.java:61)处的impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)原因:org.eclipse.emf.ecore.xmi.PackageNotFoundException:带有uri'的软件包找不到清单。 (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.int (文件:/// C:/Users/2/My%20Repository/UNIT%20Research%20and%20Development/com.unitbilisim.research.transformation/model/List.xmi,6,40),网址为org.eclipse.emf。 org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2458)上的ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2625)在org.eclipse.emf.ecore.xmi处。在org.eclipse.emf.ecore.xmi.impl.XMLHandler处的impl.XMLHandler.createObjectByType(XMLHandler.java:1335)在org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1504)在org.eclipse.emf.ecore.xmi.impl.XMLHandler位于org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMIHandler.java:77)处的processElement(XMLHandler.java:1026)位于org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler。 org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:719)处的java.1008)org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHandler.java:163)处的XMLHandler.startElement(XMLHandler.java:719) com.sun.org.apache.xerces.int上的com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(未知源) ernal.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.AbstractSAXP ern.impl.dtd.XMLDTDValidator.startElement(未知源),位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(未知源),位于com.sun.org.apache.xerces.internal.impl。 XMLDocumentScannerImpl $ ContentDriver.scanRootElementHook(未知来源)位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(未知来源)位于com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(未知来源)的com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument的PrologDriver.next(未知来源) ),位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知源)的com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知源)。 org.apache.xerces.internal.parsers.XMLParser.parse(未知源),位于com.sun.org.apache.xerces.internal.parsers.AbstractSAXP arser.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 com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(未知源)com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(未知源) org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)上的org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:261)上的Unknown Source)位于org.eclipse的org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)的org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)位于org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)的emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)... 2更多

What does 'package with uri "" not found' mean ? “未使用uri打包”是什么意思? Can I read xmi file directly or do I need to parse it as xml file ? 我可以直接读取xmi文件还是需要将其解析为xml文件?

I have also tried this -> https://stackoverflow.com/a/4615965/1604503 我也尝试过-> 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. PackageNotFoundEx。 and Reource$IOWrappedEx. 和Reource $ IOWrappedEx。 again :( 再次:(

Please help 请帮忙

kind regards 亲切的问候

The reason was, I did not generate the model codes. 原因是,我没有生成模型代码。 Because of that, 'List' package could not be found. 因此,找不到“列表”包。 I did add it to my packageRegistry and that's all. 我确实将它添加到我的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);

You should use URI.createFileURI(absolutePathUri) instead of URI.createURI(uri) . 您应该使用URI.createFileURI(absolutePathUri)而不是URI.createURI(uri) Also, your model instance (in your case it is List.xmi file) must have xsi:schemaLocation attribute with relative path to your metamodel to avoid pre-registration of your metamodel package. 另外,您的模型实例(在您的情况下,它是List.xmi文件)必须具有xsi:schemaLocation属性,该属性具有指向元模型的相对路径,以避免预先注册元模型包。 Very important note: your uri parameter must be constructed from absolute path of your model 非常重要的注意事项:您的uri参数必须从模型的绝对路径构造

Kotlin language example: Kotlin语言示例:

    Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.apply {
        put("ecore", EcoreResourceFactoryImpl())
        put("xmi", XMIResourceFactoryImpl())
    }

    val resourceSet = ResourceSetImpl()

    // createFileURI method is able to locate metamodel by xsi:schemaLocation
    // absolute path is important here !!!
    val resource = resourceSet.getResource(URI.createFileURI(File(modelPath).absolutePath), true)
    resource.allContents.forEach { eObj ->
        //do smth
    }

To make pre-registration of dynamic EPackage refer to eclipse wiki 要预先注册动态EPackage,请参阅Eclipse Wiki。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM