views:

671

answers:

2
A: 

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.

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.

EDIT

Here is a resource utility class from 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.

EDIT 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!!.

Stephen C
Can you gimme a start, what is the EMF Library which has the methods to read/write xml files?
zengr
+1  A: 

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.

ShiDoiSi
please check update2.
zengr
Yes, CDO or Teneo would be worth investigating when llooking to store EMF models in databases. Teneo: http://www.eclipse.org/modeling/emft/?project=teneo
lothar