I have a XML file resulted from an input java file. I also have xPath
expressions for the XML file.
I need a function that receives one xPath expression and return its java element (in the abstract syntax tree). I tried the below code:
First extract XML element based on the input xPath expression.
XPath xPath = XPathFactory.newInstance().newXPath(); String query = "//unit[1]/unit[1]/class[1]/block[1]/function[6]"; //a method Node node = (Node) xPath.compile(query).evaluate(XmlDocument, XPathConstants.NODE);
However, I do not know how to link extracted
XML
node to Java element in the source code.
PS:
The reslut should be a node in the abstract syntax tree. I have AST created by spoon. Therefore, in the above example, I want to extract related CtMethodImpl
.
node.getTextContent()
is not the answer as it is possible that there is more than one instance with the similar text content.
To the best of my knowledge there is no 'direct' way of doing this.
This:
"//unit[1]/unit[1]/class[1]/block[1]/function[6]"
is what we call a signature in the sense that it uniquely identifies an element (somehow).What I would do is to create a spoon processor and go through the entire AST checking each element to see if it matches the signature.