![](/img/trans.png)
[英]How to get all li tags within the class in the elements array in Jsoup
[英]Is there a standard way to get all elements with a class name WITHIN a certain node?
我知道Document.getElementsByClassname(String)
可以在整個文檔中獲取特定類名稱的所有元素。
我現在正在嘗試將那些元素放入文檔的特定節點中。 在org.w3c.dom.Element
,我僅找到getElementsByTagname
。 我真的需要遍歷所有這些元素並讀取類名,還是有更好的方法?
我正在嘗試僅使用Java查找解決方案(可以與其他庫一起使用)。
非常感謝!
我最終使用了JSoup,其中包含我需要的方法。
http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByClass%28java.lang.String%29
盡管您正在使用org.w3c.dom.Document
對象,但這也應該起作用。
我在使用xalan
庫從帶有Batik的SVG的SVG中獲取XPath的特定元素中找到了解決方案。 唯一的區別是,由於與org.w3c.dom.xpath.XPathEvaluator
實現的batik
不一致,我最終使用了XPathAPI
而不是XPathEvaluator
。
這是我編寫的代碼的相關部分:
Element searchRoot= document.getRootElement();
NodeList nl = XPathAPI.selectNodeList(searchRoot, ".//*[@class=\"class_name\"]");
jQuery是更好的方法。
這將在ID為“ nodeToSearchName”的節點中選擇所有類為“ classNameToFind”的節點。
$(“#nodeToSearchName”)。find(“。classNameToFind”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.