R4de
R4de

Reputation: 87

How to get Document Or Element (XML DOM) from Resource or EObject

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?

Upvotes: 0

Views: 39

Answers (1)

R4de
R4de

Reputation: 87

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

Upvotes: 0

Related Questions