How could I fully get EMF models from an XML resource? [message #1848834] |
Mon, 20 December 2021 05:12  |
Eclipse User |
|
|
|
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 06:31  |
Eclipse User |
|
|
|
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...
|
|
|
Powered by
FUDForum. Page generated in 0.17488 seconds