Home » Android » android – Java library using css selectors to parse XML

android – Java library using css selectors to parse XML

Posted by: admin June 15, 2020 Leave a comment


Is there a jQuery like JAVA/Android library that uses CSS Selectors to parse the XML ?

Like :

String desc = myXML.find("bloc[type=pro]").get(0).attr("description");

Chainability is also what I’m looking for, in the same way of jQuery…

I hope this exists !

How to&Answers:

While initially designed as a HTML parser with CSS selector support, Jsoup works fine for XML documents as well if your sole intent is to extract data, not to manipulate data.

Document document = Jsoup.parse(xmlString);
String desc = document.select("bloc[type=pro]").get(0).attr("description");
// ...

You see, the syntax is almost identical to what you have had in the question.


Apache Jericho is what you are looking for.

You example would look like

String desc = source.getFirstElement( "type", "pro" ).getAttributeValue( "description" );

It’s a charm to parse HTML with jericho, so I guess it’s even easier for well structured XML.


Since there are some bugs in other Libraries like Jsoup and Jericho is different from what I was expecting,

I wrote a Class Extending the org.xml.sax.helpers.DefaultHandler which parse the XML. I then wrote two other Classes that look like Element and Elements from Jsoup containing two functions called find that handle the CSS3 Selector and attr that returns the attribute value.

I’m now cleaning and commenting that code… I’ll post the library later for who is interested in.


is now possible like in jQuery !

Edit !

Here is the link to access the code for who is interested : Google Code Project

Moving to GitHub : https://github.com/ChristopheCVB/JavaXMLQuery


I use XPath to solve that issue. XML parsing like JDOM is ok to to the XPath. Maybe jQuery see how XPath works :p


Xpath index start from 1, not 0



The droidQuery library can do many of the things you are looking for. Although the syntax is a little different, you can:

  • Get view attributes using chained, jQuery-style commands, such as:

    CharSequence text = $.with(this, R.id.myTextView).attr("text");
  • Parse XML:

    Document dom = $.parseXML(myXMLString);

If you are a fan of jQuery, you will be pleased to see that nearly all of the features it provides are included in droidQuery, and although the syntax may differ at times, its major goal is to be as syntactically close to jQuery as possible.