2014-11-16 39 views
1

我試圖編寫一個方法來執行JDOM2 XPath。我想能夠在任何類型的過濾器來傳遞(例如Filter<Content>Filter<Element>。我傳遞中的元素。元素是一個擴展內容過濾的接口。編譯器警告無法從XPathExpression <Object>轉換爲XPathExpression <Content>

「的方法executeXPath(Document, String, String, Filter<Content>)在XMLUtilities類型不適用於參數(Document, String, String, Filter<Element>)

有沒有辦法做到這一點,而無需爲每種內容類型創建單獨的方法?

Filter<Element> filter = new org.jdom2.filter.ElementFilter(); 
List<Element> xPathSearchedNodes = XMLUtilities.executeXPath(doc, "/x:root","http://www.example.com",filter); 

....

static public List<Content> executeXPath(Document document, String xpathStr, String namespace, Filter<Content> filter) {...} 

回答

3

你可以讓你的方法一般:

static public <T extends Content> List<T> executeXPath(Document document, String xpathStr, String namespace, Filter<T> filter) {...} 

如果你傳遞一個Filter<Content>它返回一個List<Content>,如果你傳遞一個Filter<Element>它我會返回一個List<Element>

+0

我不得不使用「<?extends Content>」爲c ompiler不要抱怨,然後在客戶端投這個名單。 – user994165

相關問題