Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Export EMF Model to custom XML file(Export Model to custom XML file)
Export EMF Model to custom XML file [message #1693425] Fri, 24 April 2015 07:10 Go to next message
Daniel Kim is currently offline Daniel KimFriend
Messages: 2
Registered: April 2015
Junior Member
Hi,

I am currently building a EMF editing tool with GEF.
By default, the editor saves a model as XML format, but I want to export it to a XML file which is based on my own structure.

Is there any tutorial or advice on this "export" related issue?

Thanks.
Re: Export EMF Model to custom XML file [message #1693543 is a reply to message #1693425] Fri, 24 April 2015 21:49 Go to previous messageGo to next message
Nikos Margaritis is currently offline Nikos MargaritisFriend
Messages: 65
Registered: September 2014
Member

There are several ways to do this.

The simplest game is to read the model file and create a new document from it. I would recommend jdom2 for this.

You could start like that

SAXBuilder saxBuilder = new SAXBuilder(); 
\\where modelPath a string originated from the IPath of the  model file
Document originalDoc = saxBuilder.build(new File(modelPath));

\\an outputter with a pretty print format
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());	
\\the new doc that you will write too
Document exportDoc = new Document();
.....
.....your processing methods
.....
\\xmlOutputter can be used to write the xml to the file, you will need to call the .output method with the exported doc and an OutputStreamWriter


Then processing the nodes it is pretty easy, you can either use the traditional parent -> children approach or an implementation a bit more generic that is robust to model structure changes. This implementation is related with xpath expressions. There are some pros and cons in these approaches that I suggest you investigate and evaluate yourself.


Xpath expressions are used like this.
public static List<Element> getElements(String regex, Document doc, Namespace ns) {
    	XPathFactory xFactory = XPathFactory.instance();
        XPathExpression<Element> expr = xFactory.compile(regex, Filters.element(), null, ns);    
        return expr.evaluate(doc);   
 }


\\a sample caller of the method
getElements("//elementName",doc,namespace).
			forEach(el->{
                             //do your processing with lamdas
                             // e.g Element myElement = new Element("test");
                             // etc
                        });


A list of the xpath expressions and their meaning can be found here XPath Tester / Evaluator / Samples


More advanced steps:


  • write a command handler and call the jdom2 xml tranformer class from there
  • append this command to your editor menu or while right clicking on the model file or the emf editor etc by using the eclipse plugin extension points,


Note that, this question is not really related with GMF.

Best, Nikos.

[Updated on: Fri, 24 April 2015 21:58]

Report message to a moderator

Re: Export EMF Model to custom XML file [message #1695119 is a reply to message #1693543] Tue, 12 May 2015 07:30 Go to previous message
Daniel Kim is currently offline Daniel KimFriend
Messages: 2
Registered: April 2015
Junior Member
Thanks for your reply.
It helps me a lot!

Thanks.
Previous Topic:GMF Help
Next Topic:OCL and GMF graphic instance
Goto Forum:
  


Current Time: Fri Oct 20 10:51:05 GMT 2017

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

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