|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03919 seconds