Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parsing XML
Parsing XML [message #1826154] Mon, 20 April 2020 11:39 Go to next message
Francisco Martínez Lasaca is currently offline Francisco Martínez LasacaFriend
Messages: 2
Registered: April 2020
Junior Member
Hi,

I have a simple meta-model for a concrete XML subset. I have to implement a simple description tag which includes arbitrary text inside.

This is a simple example:
<?xml version="1.0"?>
<package format="3">
  <name>my_name</name>
  <version>1.0.0</version>
  <description>This is the description</description>
  ...


I have created this EMF class

class DescriptionTag extends Tag {
  attr String[1] description;
}


along with this Xtext implementation

DescriptionTag returns DescriptionTag:
	"<" 'description' ">" description=STRING "<" "/" 'description' ">";


the problem is that STRINGs in Xtext are quoted, and I would like to get rid of those (bloody) quotes.

This is an example of a valid (yet not desired) parsed excerpt of a file:
  <description>"This is the description"</description>


I have tried redefining the grammar (that STRING comes from org.eclipse.xtext.common.Terminals).

Any idea?

Thanks
Re: Parsing XML [message #1826178 is a reply to message #1826154] Mon, 20 April 2020 16:34 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 119
Registered: February 2016
Senior Member
Hello Francisco!

For parsing XML with Xtext, you can take a look at the Eclipse GEF project.
The GEF DOT editor is an Xtext-based editor and it is capable of parsing Graphviz HTML-Like labels.

For further information you can study the following blog posts and videos:
- Graphical Editing Framework (GEF) 5.0.0 Release
- Editing Graphviz *.dot files with the GEF DOT Editor
- GEF 5.0.0 - From a User's Perspective (at 09:32)
- Eclipse GEF DOT - Graphviz authoring environment in Eclipse (at 21:03)


The corrsponding Xtext grammar files are available under:
- Dot.xtext
- DotHtmlLabel.xtext

Hope that helps,
Tamás

[Updated on: Mon, 20 April 2020 16:44]

Report message to a moderator

Re: Parsing XML [message #1826192 is a reply to message #1826178] Mon, 20 April 2020 19:42 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6934
Registered: July 2009
Senior Member
Hi

Do you really want to 'parse' XML? The problem is very well solved by the SAXParser and integrated by EMF.

If you want more insight into the XML structure you might consider the /org.eclipse.qvtd.xml/model/XMLmodel.ecore that forms parts of the QVTd distribution. It creates a usable Ecore model instance of the XML, using its own overload of the SAXParser.

If you really really want to parse XML, I can point you at some code that produces a custom Xtext / LPG parser from the Ecore metamodel of your XML. I have not progressed it much since while such an optimized parser is quite a bit faster, in practice it isn't since the optimized parser is custom and so needs to warm up the JVM whereas the slower EMF parser is probably already warm from previous usage. The custom parser will only really be an unequivocal win once generated as C code.

Regards

Ed Willink

Re: Parsing XML [message #1827906 is a reply to message #1826154] Tue, 26 May 2020 16:42 Go to previous messageGo to next message
Francisco Martínez Lasaca is currently offline Francisco Martínez LasacaFriend
Messages: 2
Registered: April 2020
Junior Member
Hi all,

Sorry for answering back so late.

Thanks for all your resources.

I am not worried about parsing entire XML files (I have a really concrete subset of tags I'm working with), but getting rid of the quotes I mention.

Thanks.

[Updated on: Tue, 26 May 2020 16:57]

Report message to a moderator

Re: Parsing XML [message #1827909 is a reply to message #1827906] Tue, 26 May 2020 17:54 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6934
Registered: July 2009
Senior Member
HI

Well going back to your original comment:
Quote:
I have to implement a simple description tag which includes arbitrary text inside.

'simple' and 'arbitrary' are a contradiction.
If you need arbitrary text then you have to have an escape mechanism to distinguish awkward characters with the arbitrary text from the same characters as subsequent text. The double quotes and backslashing is one such escape mechanism. Of course if you really like XML, you could use CDATA instead. That may provoke a stronger expletive.

Regards

Ed Willink
Previous Topic:Synchronize with resources
Next Topic:How to infer an annotation with a key-value pair
Goto Forum:
  


Current Time: Mon Nov 30 21:27:14 GMT 2020

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

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

Back to the top