0

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?

Banoffee
8646 silver badges13 bronze badges
asked Jul 19, 2024 at 10:16
3
  • 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. Commented 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. Commented 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. Commented Jul 19, 2024 at 15:41

1 Answer 1

0

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

answered Jul 19, 2024 at 15:44
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.