Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Load EMF instance model from xmi file and traverse it
icon4.gif  Load EMF instance model from xmi file and traverse it [message #1474535] Sat, 15 November 2014 13:59 Go to next message
furkan tanriverdi is currently offline furkan tanriverdiFriend
Messages: 15
Registered: November 2014
Junior Member
Let's assume I have an ecore meta-model name A.ecore. I create dynamic instance from it and I name it a.xmi. What I want to do is, traverse the xmi file and figure out which EClasses elements refer to. I mean, I need know their types. What are the attributes they have and values. What are the EReferences they have and target types.

Is that possible ?

I followed one way like this:

try {
			 
			File fXmlFile = new File("model/Collection.xmi");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
		 
			//optional, but recommended
			//read this - stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
			doc.getDocumentElement().normalize();
		 
			System.out.println("Root element :" + doc.getDocumentElement().getNodeName()+"\n");
		 
		 
			NodeList nodeList = doc.getElementsByTagName("*");
		    for (int i = 0; i < nodeList.getLength(); i++) {
		        Node node = nodeList.item(i);
		        if (node.getNodeType() == Node.ELEMENT_NODE) {
		            // do something with the current element
		        	
		        }
		    }
		    
		    } catch (Exception e) {
			e.printStackTrace();
		    }


But I do not think this is the right way to do what I want.

Could you please show me an efficient way ??

I will be thankful If anyone help Smile

thanks
regards
Re: Load EMF instance model from xmi file and traverse it [message #1474571 is a reply to message #1474535] Sat, 15 November 2014 14:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Furkan,

Comments below.

On 15/11/2014 2:59 PM, furkan tanrıverdi wrote:
> Let's assume I have an ecore meta-model name A.ecore. I create dynamic
> instance from it and I name it a.xmi. What I want to do is, traverse
> the xmi file and figure out which EClasses elements refer to. I mean,
> I need know their types. What are the attributes they have and values.
> What are the EReferences they have and target types.
> Is that possible ?
>
> I followed one way like this:
>
> try {
> File fXmlFile = new
> File("model/Collection.xmi");
> DocumentBuilderFactory dbFactory =
> DocumentBuilderFactory.newInstance();
> DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
> Document doc = dBuilder.parse(fXmlFile);
> //optional, but recommended
> //read this -
> stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
> doc.getDocumentElement().normalize();
> System.out.println("Root element :" +
> doc.getDocumentElement().getNodeName()+"\n");
> NodeList nodeList =
> doc.getElementsByTagName("*");
> for (int i = 0; i < nodeList.getLength(); i++) {
> Node node = nodeList.item(i);
> if (node.getNodeType() == Node.ELEMENT_NODE) {
> // do something with the current element
>
> }
> }
> } catch (Exception e) {
> e.printStackTrace();
> }
> But I do not think this is the right way to do what I want.
Well, it's just so much XML noise. No doubt you want to build an EMF
instance from it.
> Could you please show me an efficient way ??
I get the sense you've not so much followed any of the tutorials yet...
It's probably a good idea that you take your A.ecore and follow one of
the tutorials to generate a model for it and to generate the example
code for reading and writing instances...
> I will be thankful If anyone help :)
> thanks
> regards


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Load EMF instance model from xmi file and traverse it [message #1474701 is a reply to message #1474571] Sat, 15 November 2014 17:15 Go to previous messageGo to next message
furkan tanriverdi is currently offline furkan tanriverdiFriend
Messages: 15
Registered: November 2014
Junior Member
I know generating genmodel and model, editor and other plugins. What I want to know is can I make inferences from an instance model. I am trying get EClasses, EAttributes from instance. Those codes just a different try. I hope I could explain myself
Re: Load EMF instance model from xmi file and traverse it [message #1475413 is a reply to message #1474701] Sun, 16 November 2014 07:37 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Furkan,

Comments below.

On 15/11/2014 6:15 PM, furkan tanrıverdi wrote:
> I know generating genmodel and model, editor and other plugins.
So you know how to load and save instances, but you show code that's
trying to read using DOM. Why is that?
> What I want to know is can I make inferences from an instance model.
Yes, of course. EObject.eClass will return the object's EClass.
Resource.getAllContents returns an iterator that walks all instances in
a resource.
> I am trying get EClasses, EAttributes from instance.
EClass.getEAllAttributes.
> Those codes just a different try. I hope I could explain myself


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Load EMF model instance from XMI file
Next Topic:[oomph] adding new setup will not add git repositories to view
Goto Forum:
  


Current Time: Fri Apr 19 01:30:37 GMT 2024

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

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

Back to the top