Home » Modeling » EMF » Accessing the content of an EReference(How can I get the content of an EReference? )
Accessing the content of an EReference [message #1445678] |
Wed, 15 October 2014 15:48  |
Eclipse User |
|
|
|
All,
This question probably has an easy answer, but I'm not seeing it. A kick in the head would be appreciated.
How can I get the content of an EReference? (See element below)
Assuming I have the XMI loaded into an object graph, I can get the element shown as an EReference, but cannot find a way of accessing the "Good..." part. If I downcast to the specific datatype, STImpl, in this case; then it's easy, but that introduces a plethora of difficulties, which I'd like to avoid.
The element:
<title>Good Things Happen</title>
Thanks
|
|
|
Re: Accessing the content of an EReference [message #1445926 is a reply to message #1445678] |
Thu, 16 October 2014 01:12   |
Eclipse User |
|
|
|
Geoffry,
On 15/10/2014 9:48 PM, Geoffry Roberts wrote:
> All,
>
> This question probably has an easy answer, but I'm not seeing it. A
> kick in the head would be appreciated.
I'll try to aim higher. :-P
>
> How can I get the content of an EReference? (See element below)
You have a generated API?
> Assuming I have the XMI loaded into an object graph, I can get the
> element shown as an EReference,
What you show below looks like an EAtrributes's value... But I suppose
they're IDs?
> but cannot find a way of accessing the "Good..." part.
> If I downcast to the specific datatype, STImpl, in this case;
That's an EClass not an EDataType.
> then it's easy, but that introduces a plethora of difficulties, which
> I'd like to avoid.
>
> The element:
> <title>Good Things Happen</title>
It's impossible to say without more knowledge of your model. But
presumably you have some object with a feature called "title". If it's
really an EReference then getTitles() will hold three objects and the
IDs of those objects will be Good, Things, and Happens. I can't say
what type of object (or even if the object has such a feature; it could
just be stored in the XMIResourceImpl's ID map).
>
> Thanks
|
|
|
Re: Accessing the content of an EReference [message #1446275 is a reply to message #1445926] |
Thu, 16 October 2014 11:59   |
Eclipse User |
|
|
|
Ed,
I tried to walk the line between excessive information and too little. Seems like I erred on the side of too little.
The model in question is an inheritance. Surprisingly, in all my EMF work heretofore this little issue has never come up. I must have led a sheltered life.
The code that follows is a bit that loops the first level of children in the model. I know my <title> element is an EReference because it prints out in the second loop, which as you can see, loops through the references. Many of the references have no content, but certain ones do. I need to access that content as a String from an EReference.
What do you mean "getTitles()"?
// eObject is the top of the model; the document from an XML perspective.
EClass eClass = eObject.eClass();
for (EAttribute eAttribute : eClass.getEAllAttributes()) {
Object value = eObject.eGet(eAttribute);
log.info("eAttribute=" + eAttribute.getName() + " value=" + value;
}
for (EReference eReference : eClass.getEAllReferences()) {
Object value = eObject.eGet(eReference, true);
log.info("eReference=" + eReference.getName() + " value=" + value;
}
The output when <title> is encountered:
eReference=title value=org.openhealthtools.mdht.uml.hl7.datatypes.impl.STImpl@4d3266b4 (nullFlavor: <unset>) (representation: <unset>) (mixed: [ecore.xml.type:text=Good Health Health Summary], mediaType: <unset>, language: null, compression: <unset>, integrityCheck: null, integrityCheckAlgorithm: <unset>)
|
|
|
Re: Accessing the content of an EReference [message #1446329 is a reply to message #1446275] |
Thu, 16 October 2014 13:42  |
Eclipse User |
|
|
|
Geoffry,
Comments below.
On 16/10/2014 5:59 PM, Geoffry Roberts wrote:
> Ed,
>
> I tried to walk the line between excessive information and too
> little. Seems like I erred on the side of too little.
It's a fine line. :-P
>
> The model in question is an inheritance. Surprisingly, in all my EMF
> work heretofore this little issue has never come up. I must have led
> a sheltered life.
>
> The code that follows is a bit that loops the first level of children
> in the model. I know my <title> element is an EReference because it
> prints out in the second loop, which as you can see, loops through the
> references. Many of the references have no content, but certain ones
> do. I need to access that content as a String from an EReference.
>
> What do you mean "getTitles()"?
Given knew nothing of your model and given that the tags in the XML
correspond to features, I have to assume there is a feature called
"title" in your model, and that generally means you'll have a getter
like getTitle or getTitles....
> // eObject is the top of the model; the document from an XML
> perspective. EClass eClass = eObject.eClass();
> for (EAttribute eAttribute : eClass.getEAllAttributes()) {
> Object value = eObject.eGet(eAttribute);
> log.info("eAttribute=" + eAttribute.getName() + " value=" + value;
> }
> for (EReference eReference : eClass.getEAllReferences()) {
> Object value = eObject.eGet(eReference, true);
> log.info("eReference=" + eReference.getName() + " value=" + value;
> }
>
> The output when <title> is encountered:
> eReference=title
> value=mailto:org.openhealthtools.mdht.uml.hl7.datatypes.impl.STImpl@4d3266b4
> (nullFlavor: <unset>) (representation: <unset>) (mixed:
> [ecore.xml.type:text=Good Health Health Summary], mediaType: <unset>,
> language: null, compression: <unset>, integrityCheck: null,
> integrityCheckAlgorithm: <unset>)
This tells me quite a bit more about your model. It's XML Schema
based. I see that this "ST" has mixed content, and the value you're
asking about is stored in that st.getMixed(). You'll find this article
useful for understand what can be in the mixed content and how to
process feature maps.
http://www.theserverside.com/news/1364302/Binding-XML-to-Java
In this case, st.getMixed().get(0).getValue wil be that string value,
but mixed content can contain lots of other things, including nested
elements... See the "Loading and Traversing EMF AnyType" section of the
above article.
|
|
|
Goto Forum:
Current Time: Fri Apr 18 01:15:27 EDT 2025
Powered by FUDForum. Page generated in 0.03038 seconds
|