开发者

Finding Elements by Attributes in a DOM Document [closed]

Closed. This question is not reproducible or was caused by typos. It is not curre开发者_运维技巧ntly accepting answers.

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 question

How 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜