Jsoup – search for elements by attribute value
•
Java
So I'm not sure if this is feasible But I want to scan the XML document to find all elements with specific attribute values
It doesn't matter what elements or attribute types are... I just need to find them based on attribute values
For example, I'm looking for the word "duck"
<person name="Fred" thing="duck"/> <person name="Mary"/> <animal name="duck" thing="swims"/>
The first and third should match and the second does not
Any ideas?
Thank you.
Solution
If the selector can do this, it's uncomfortable But maybe you can try something like this:
final String input = "<person name=\"Fred\" thing=\"duck\"/>" + "<person name=\"Mary\"/>" + "<animal name=\"duck\" thing=\"swims\"/>"; Document doc = Jsoup.parse(input); Elements withAttr = new Elements(); for( Element element : doc.getAllElements() ) { for( Attribute attribute : element.attributes() ) { if( attribute.getValue().equalsIgnoreCase("duck") ) { withAttr.add(element); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码