Finding Elements by Attributes in a DOM Document [closed]
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this questionHow to find elements by attributes in a DOM document with Java? Thanks.
With a utility like jOOX, you can wrap your DOM document and use a jQuery-like API to achieve what you need. You have several options:
Using XPath (of course, instead of using jOOX, you could use the standard Java XPath API directly)
Match match = $(document).xpath("//element[@attrName='value']");
Using CSS-style selectors
Match match = $(document).find("element[attrName='value']");
Using the jOOX API
Match match = $(document).find("element").filter(attr("attrName", "value"));
The returned org.joox.Match
object can then be used in various ways, e.g. for iteration:
for (Element element : match.each()) {
System.out.println($(element));
}
I have no idea what the options are as far as the XML/DOM parsing in Java, and I don't know what (if anything) you're using currently, but, if you have some way of finding elements via CSS selectors (a common feature in js frameworks), then CSS attribute selectors would be the way to go.
Google turned this Java implementation of CSS selectors up, perhaps it'll help.
精彩评论