XPath with XmlFile (PsiFile)

Hi ! I was searching for a few hours how to execute an XPath search using the Psi from Idea, but I couldn't find anything (just some methods like XmlUtil.findOnAnyLevel that could help in very simple xpaths).
Is there any way for searching with XPath on an .xml file and get the PsiElements as the result ?

Thanks !
Enrique.

3 comments
Comment actions Permalink

There is no support for XPath searches directly in the XML PSI API. You can look at how the XPath plugin implements its actions for XPath evaluation (XPathEvalAction) but generally that's an approach that's targeted for users, not plugin developers.

0
Comment actions Permalink

I don't have that class in my source files, and thats weird because I can see it in some imports :

import org.intellij.plugins.xpathView.XPathEvalAction;


I have a helper for searching an xpath right now:

public static List<Element> findXpath(File xmlFile, String xpath)
    {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document build = builder.build(xmlFile);
            XPath filterXpression = XPath.newInstance(xpath);
            // String xpathCompiled = filterXpression.getXPath();
            List<Element> nodes = filterXpression.selectNodes(build);
            return nodes;
        } catch (JDOMException e) {
            e.printStackTrace();  
        } catch (IOException e) {
            e.printStackTrace();  
        }
        return null;
    }


but it returns Element not PsiElement, can I convert that Element into a PsiElement?

Thanks !
0
Comment actions Permalink

If you're working on a plugin, you'll need to add the XPathView plugin jar to the classpath of the IntelliJ IDEA SDK.

DOM elements and PSI elements are two completely different representations of an XML document; there is no way to convert between them.

0

Please sign in to leave a comment.