Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How could I fully get EMF models from an XML resource?(How could I fully get EMF models from an XML resource?)
How could I fully get EMF models from an XML resource? [message #1848834] Mon, 20 December 2021 10:12 Go to next message
Weixing Zhang is currently offline Weixing ZhangFriend
Messages: 19
Registered: April 2021
Junior Member
I have an XML with some EMF models in the hierarchy, for example:
Quote:
topPackage
----packageA
---------element1
---------element2
----packageB
...

Here I could only get topPackage and packageA,B, but couldn't get element1 and element2 (shows null).
Below is my code:
public EObject getEMFModel(String filePath, XtextResourceSet xtextResourceSet) {
		// We need to make sure that EMF knows where to find the right resource factory
		xtextResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("myxml",
				new MyEAToolResourceFactoryImpl());
		xtextResourceSet.getPackageRegistry().put(MyEAToolPackage.eNS_URI, MyEAToolPackage.eINSTANCE);
		URI openUri = URI.createFileURI(new File(filePath).getAbsolutePath());

		// We are now getting an instance of MyEAToolResourceImpl here
		Resource xmlResource = xtextResourceSet.getResource(openUri, true);

		// And calling load() will deserialise the MyXML into an in-memory model
		try {
			xmlResource.load(null);
		} catch (IOException e) {
			logger.warn("MyXML file " + filePath + " could not be loaded.", e);
			return null;
		}

		EObject topObject = xmlResource.getContents().get(0);

		return topObject;
	}

So how could I fix it?
Re: How could I fully get EMF models from an XML resource? [message #1848835 is a reply to message #1848834] Mon, 20 December 2021 11:31 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
The call to load is pointless because your getResource call has a second argument of true which means the returned resource will already have been loaded. Then you access the root object in the resource. You show nothing beyond that point and I don't know what type of object you are expecting. You can use xmlResource.getAllContents to iterate over the entire tree of objects contained by the resource...

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Increasing prefetch performance
Next Topic:Model and meta model
Goto Forum:
  


Current Time: Wed May 01 23:42:23 GMT 2024

Powered by FUDForum. Page generated in 0.03512 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top