How to get Document or Element (XML DOM) from Resource or EObject?
Loading of resource is done this way
URI uri = URI.createFileURI(file.getPath());
Resource resource = resourceSet.getResource(uri, true);
models.add((MODEL_CLASS) resource.getContents().get(0));
File path is path of XML file. As XML schema version can change, model can have different methods, e.g., model.getA() can be model.getB() depending of the TAG name in XML. I would like to handle that case with simple if statement by checking the version, for example
if (version == version_1) {
xml_model.getElementsByTagName('TAG_A')[0]
} else {
xml_model.getElementsByTagName('TAG_B')[0]
}
Is something like this possible? If not, is there any other solution for this problem?
-
There is an EMF2DOM adapter in the WTP Common's org.eclipse.wst.common.emf bundle, but I don't know much more than that about it. It's included in the download.eclipse.org/releases/latest update site.nitind– nitind2024年07月19日 12:42:07 +00:00Commented Jul 19, 2024 at 12:42
-
I don't understand your questions. "How to get Document or Element (XML DOM) from Resource or EObject?" => Resource or EObject has no dependency to XML. My guess is you have a serialized form that conforms to the XMI format. XMI can be read just like XML. "As XML schema version can change" => Your use case is very unclear. But if you want to tweak how your EMF models are serialized, there is a host of resources (in particular the EMF book) regarding this subject.Banoffee– Banoffee2024年07月19日 13:38:34 +00:00Commented Jul 19, 2024 at 13:38
-
Thanks for advises, I am not that well experienced with EMF, XML, XMI etc., I tried to download and use WTP but I couldn't do it. Anyway, it might help others, as I saw some usage of it. eclipse.org/forums/index.php/t/134338 eclipse.org/forums/index.php/t/39467 Meta model that I am using is provided to me. I don't think that I can solve problem by changing EMF model. I could accomplish what I wanted with java reflection method call as I needed to call different method depending of which xml schema (and jar corresponding to metamodel) is used.R4de– R4de2024年07月19日 15:41:02 +00:00Commented Jul 19, 2024 at 15:41
1 Answer 1
Simple usage of java reflection can solve this problem.
Depending on the different version of metamodel (provided as jar), method name that should be called is chosen. Version of metamodel/jar is passed as command line argument.
Java method reflection tutorial that I followed: https://www.baeldung.com/java-method-reflection