eclipse 如何使用 EMF 读取 XML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2543987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:34:56  来源:igfitidea点击:

How to use EMF to read XML file?

javaxmleclipseeclipse-emf

提问by zengr

EMF = Eclipse Modeling Framework

EMF = Eclipse 建模框架

I have to use EMF in one of my class projects. I am trying to understand how to use EMF to do the following:

我必须在我的一个课堂项目中使用 EMF。我试图了解如何使用 EMF 执行以下操作:

  1. Read XML,
  2. Get the values into objects.
  3. Use ORM to persist the values in objects to database. - Done
  4. Get data from database using ORM and generate XML.
  1. 读取 XML,
  2. 将值放入对象中。
  3. 使用 ORM 将对象中的值持久化到数据库中。- 完毕
  4. 使用 ORM 从数据库中获取数据并生成 XML。

I need to do all of that using: EMF (no idea what so ever) and JPA (DONE).

我需要使用:EMF(不知道是什么)和JPA(完成)来完成所有这些。

I have used JAXB and I know, this can be done using JAXB, but how is (EMF == JAXB)?!

我使用过 JAXB 并且我知道,这可以使用 JAXB 来完成,但是 (EMF == JAXB) 怎么样?!

I have created many java classes using EMF, but there are so many of them! Where do I implement the read/write methods and how do I run the EMF project?

我已经使用 EMF 创建了许多 java 类,但是它们太多了!我在哪里实现读/写方法以及如何运行 EMF 项目?

Thanks

谢谢

UPDATE1HINT http://www.eclipsezone.com/eclipse/forums/t58829.html

UPDATE1提示 http://www.eclipsezone.com/eclipse/forums/t58829.html

UPDATE2

更新2

I have schema and I have generated the model code using the .xsd. Now I am having problem in reading the data from the XML file.

我有架构,并且使用 .xsd 生成了模型代码。现在我在从 XML 文件读取数据时遇到问题。

采纳答案by Hendy Irawan

EMF serializes the data model using (the default, most popular way) XMIResourceImpl, which strictly uses the XMI format, and not a custom XML structure.

EMF 使用(默认的、最流行的方式)XMIResourceImpl 序列化数据模型,它严格使用 XMI 格式,而不是自定义 XML 结构

See http://eclipsedriven.blogspot.com/for articles about EMF and its use cases.

有关 EMF 及其用例的文章,请参阅http://eclipsedriven.blogspot.com/

I agree with other answerers that EMF is probably not what you want here.

我同意其他回答者的意见,即 EMF 可能不是您想要的。

If I really want to use EMF with a custom XML structure (i.e. not XMI), I'd probably use JAXB to marshal/unmarshal my EMF objects (EObjects).

如果我真的想使用带有自定义 XML 结构(即不是 XMI)的 EMF,我可能会使用 JAXB 来编组/解组我的 EMF 对象(EObjects)。

So EMF is not the same as JAXB and they have different purposes and goals. In fact you probably can combine EMF and JAXB in some way. I've never tried it, but it seems there are valid uses cases. (as I said above, for marshaling/unmarshaling EMF objects to/from XML)

所以 EMF 与 JAXB 不同,它们有不同的目的和目标。事实上,您可能可以以某种方式组合 EMF 和 JAXB。我从未尝试过,但似乎有有效的用例。(正如我上面所说,用于将 EMF 对象编组/解组到 XML 或从 XML 编组)

To understand EMF you need to switch your paradigm. For a start remove the "XML" part, let it go from your mind. Then I suggest you to do the following:

要了解 EMF,您需要转换您的范式。首先删除“XML”部分,让它从你的脑海中消失。那么我建议你做以下事情:

  1. Create an empty EMF Project.
  2. Create a simple Ecore file (.ecore), or get it from the Internet. Or import from any XML Schema/UML file into an Ecore model.
  3. Right click on an EClass then "Create Dynamic Instance", this will create an .xmi file.
  4. Open the .xmi file, using Eclipse, this will open the EMF Editor for that model. Edit as you see fit.
  5. Inspect the contents of the (edited) .xmi file. You'll see that the format conforms exactly to the Ecore model. You will not be able to change/customize the mapping from model <-> XMI file because, as the name implies, XMIResourceImpl only reads/saves XMI files (which happens to be implemented on top of XML) but it does not read/save arbitrary XML format.
  1. 创建一个空的 EMF 项目。
  2. 创建一个简单的 Ecore 文件 (.ecore),或从 Internet 获取。或者从任何 XML Schema/UML 文件导入到 Ecore 模型中。
  3. 右键单击 EClass,然后单击“创建动态实例”,这将创建一个 .xmi 文件。
  4. 使用 Eclipse 打开 .xmi 文件,这将打开该模型的 EMF 编辑器。根据您的需要进行编辑。
  5. 检查(编辑的).xmi 文件的内容。您会看到格式完全符合 Ecore 模型。您将无法更改/自定义来自模型 <-> XMI 文件的映射,因为顾名思义,XMIResourceImpl 仅读取/保存 XMI 文件(恰好在 XML 之上实现)但不读取/保存任意 XML 格式。

EMF has support for reading/writing XML Schema metamodels(i.e. XSD files) for the purpose of converting them to an Ecore metamodel, but not XML files that conforms to such a schema (not even to Ecore), unless these files are in XMI format.

EMF 支持读取/写入XML 模式元模型(即 XSD 文件),以便将它们转换为 Ecore 元模型,但不支持符合此类模式(甚至不符合 Ecore)的 XML 文件,除非这些文件是 XMI 格式.

For persisting EMF models into a relational database, see: http://eclipsedriven.blogspot.com/2010/12/persisting-emf-objects-to-rdbms-with.html

要将 EMF 模型持久化到关系数据库中,请参阅:http: //eclipsedriven.blogspot.com/2010/12/persisting-emf-objects-to-rdbms-with.html

I'm using Teneo. (and optionally, CDO)

我正在使用 Teneo。(以及可选的 CDO)

Regarding "XML", if you stick to XMI and be content with it, EMF will make your life mucheasier, as you don't need to do anymapping (unlike JAXB). The downside is that our objects must be EMF Objects.

关于“XML”,如果您坚持使用 XMI 并满足于它,EMF 将使您的生活轻松,因为您不需要进行任何映射(与 JAXB 不同)。缺点是我们的对象必须是 EMF 对象。

EMF Objects are objects that are generated by EMF and implements EObject and directly or indirectly extends EObjectImpl. You usually wouldn't want to code EMF Objects by hand, it would be a chore. On the other hand, JAXB "objects" are usually pure domain object POJOs and has no additional requirement from JAXB. Note that unlike JAXB, EMF Objects are not POJOs.This explains another different goal between EMF and JAXB. Their overlap is less than you'd imagine.

EMF 对象是由 EMF 生成并实现 EObject 并直接或间接扩展 EObjectImpl 的对象。您通常不想手动编写 EMF 对象,这会很麻烦。另一方面,JAXB“对象”通常是纯域对象 POJO,对 JAXB 没有额外要求。请注意,与 JAXB 不同,EMF 对象不是 POJO。这解释了 EMF 和 JAXB 之间的另一个不同目标。他们的重叠比你想象的要少。

Reading from XMI (not XML) is very easy:

从 XMI(不是 XML)读取非常容易:

XMIResource resource = new XMIResourceImpl(URI.create("file:/path/to/mymodel.xmi"));
resource.load(null);
System.out.println( resource.eContents().get(0) );

回答by Zsolt T?r?k

You can read arbitrary XML files with EMF, provided you have the XSD for them, and you don't even have to generate Java classes from the XSD.
I blogged about this a couple of months ago, but I will paste the code snippets here as well. For a slightly more detailed explanation see my blog post on How to load and compare arbitrary XML files with EMF.

您可以使用 EMF 读取任意 XML 文件,前提是您拥有它们的 XSD,而且您甚至不必从 XSD 生成 Java 类。
几个月前我写了一篇关于这个的博客,但我也会在这里粘贴代码片段。有关更详细的解释,请参阅我关于如何使用 EMF 加载和比较任意 XML 文件的博客文章。

First you need to load the XSD and initialize a couple of things:

首先你需要加载 XSD 并初始化一些东西:

// generate EPackages from schemas
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
Collection generatedPackages = xsdEcoreBuilder.generate(schemaURI);

// register the packages loaded from XSD
for (EObject generatedEObject : generatedPackages) {
    if (generatedEObject instanceof EPackage) {
        EPackage generatedPackage = (EPackage) generatedEObject;
        EPackage.Registry.INSTANCE.put(generatedPackage.getNsURI(),
            generatedPackage);
    }
}

// add file extension to registry
ResourceFactoryRegistryImpl.INSTANCE.getExtensionToFactoryMap()
    .put(MY_FILE_EXTENSION, new GenericXMLResourceFactoryImpl());

After that you can load your XML files like you would normally do:

之后,您可以像往常一样加载 XML 文件:

ResourceSet resourceSet = ResourceSetFactory.createResourceSet();
Resource resource = resourceSet.getResource(xmlURI, true);
resource.load(Collections.EMPTY_MAP);
EObject root = resource.getContents().get(0);

回答by Andrei

I built a small program that does some basic model format conversion using emftriple (former emf4sw), i.e. Ecore XMI (XML file) to RDF TTL.

我构建了一个小程序,它使用 emftriple(以前的 emf4sw)进行一些基本的模型格式转换,即 Ecore XMI(XML 文件)到 RDF TTL。

I used some of the tips in the answers here, so thank you guys!

我在这里使用了答案中的一些提示,所以谢谢你们!

Maybe the source code can inspire you: https://github.com/frostyandy2k/model-transformer

也许源代码可以启发你:https: //github.com/frostyandy2k/model-transformer

回答by Stephen C

The normal way to do this kind of thing is to start with an XML schema, then use this to generate the EMF model, and then generate Java code. This is describe in this EMF tutorial.

做这种事情的正常方法是从一个 XML 模式开始,然后用它生成 EMF 模型,然后生成 Java 代码。这在本 EMF 教程中有所描述。

You don't need to implement read/write methods. Rather you make use of EMF library code to do the XML import and export. I don't have my copy of the EMF book in front of me right now, but it is covered in there. Alternatively, take a look at what the generated EMF editor does when you load or save a model instance.

您不需要实现读/写方法。而是使用 EMF 库代码来执行 XML 导入和导出。我现在没有 EMF 书的副本在我面前,但里面有它。或者,查看生成的 EMF 编辑器在加载或保存模型实例时的作用。

EDIT

编辑

Here is a resource utility classfrom an EMF-based project I developed a couple of years ago. Look at the load*methods. Note that this is not necessarily the best way to do this ... but it worked for me.

这是我几年前开发的基于 EMF 的项目的资源实用程序类。看load*方法。请注意,这不一定是最好的方法……但它对我有用。

EDIT 2

编辑 2

I just realised that I was using XMI not plain XML. Configuring an XML reader with a custom schema and (potentially) a customized schema mapping is likely to be different. So, returning to my original answer, look at the generated code!!.

我刚刚意识到我使用的是 XMI 而不是普通的 XML。使用自定义模式和(可能)自定义模式映射配置 XML 阅读器可能会有所不同。所以,回到我原来的答案,看看生成的代码!!.

回答by Volker Stolz

EMF is not designed to read arbitrary XML files. EMF uses XML as a backend. Where do your XML files come from/which schema do they conform to? If you have the schema, then you can use it to create an EMF model, which in turn is hopefully able to read your XML files.

EMF 并非旨在读取任意 XML 文件。EMF 使用 XML 作为后端。您的 XML 文件来自哪里/它们符合哪种模式?如果您有模式,那么您可以使用它来创建 EMF 模型,而该模型又有望能够读取您的 XML 文件。

Otherwise you would have to reverse engineer an EMF model that would indeed serialize to your XML, so that you'd be able to read it, but that would be a hack.

否则,您将不得不对一个确实会序列化为您的 XML 的 EMF 模型进行逆向工程,以便您能够读取它,但这将是一种黑客行为。

After UPDATE2:

UPDATE2 后

I guess the next question then is "What problem do you have with loading your files". How do you do it, and what is not working? Or you don't know how to do it?

我想下一个问题是“加载文件有什么问题”。你是怎么做的,什么不起作用?或者你不知道怎么做?

You first have to configure the factory for your resource set (the in-memory representation of the set of files that you want to work with), and then load the file with getResource(URI, bool). Example for some UML stuff I'm doing:

您首先必须为您的资源集(您要使用的文件集的内存表示)配置工厂,然后使用 getResource(URI, bool) 加载文件。我正在做的一些 UML 东西的例子:

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
resourceSet.getPackageRegistry().put("http://www.eclipse.org/uml2/2.0.0/UML", UMLPackage.eINSTANCE);
Resource r = resourceSet.getResource(uriToYourFile,true);
YourModelRootType root = (YourModelRootType) r.getContents().get(0);

That should all be covered in the tutorial that you already have the link to. Then, you can use the generated Java API to work from 'root'.

这些都应该包含在您已经拥有链接的教程中。然后,您可以使用生成的 Java API 从“root”开始工作。

I have no experience with the ORM-stuff, though, but maybe you should take a look at CDO.

不过,我对 ORM 的东西没有经验,但也许您应该看看CDO