How to use EMF to read XML file?

后端 未结 5 1546
别那么骄傲
别那么骄傲 2020-12-15 00:40

EMF = Eclipse Modeling Framework

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

  1. Read XML,
5条回答
  •  心在旅途
    2020-12-15 00:57

    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.

    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.

    After 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:

    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'.

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

提交回复
热议问题