Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

I'm a Java and SQL enthusiast developer working for a leading company in the E-Banking field in Zurich, Switzerland. I'm the creator of JOOQ, a comprehensive SQL library for Java, and I'm blogging mostly about these three topics: Java, SQL and JOOQ Lukas is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

CSS selectors in Java

02.04.2012
Email
Views: 2715
  • submit to reddit

CSS selectors are a nice and intuitive alternative to XPath for DOM navigation. While XPath is more complete and has more functionality, CSS selectors were tailored for HTML DOM, where the document content is usually less structured than in XML.

Here are some examples of CSS selector and equivalent XPath expressions:

CSS:   document > library > books > book
XPath: //document/library/books/book

CSS:   document book
XPath: //document//book

CSS:   document book#id3
XPath: //document//book[@id='3']

CSS:   document book[title='CSS for dummies']
XPath: //document//book[@title='CSS for dummies']

 

This becomes more interesting when implementing pseudo-selectors in XPath:

CSS:   book:first-child
XPath: //book[not(preceding-sibling::*)]

CSS:   book:empty
XPath: //book[not(*|@*|node())]

 

A very nice library that allows for parsing selector expressions according to the w3c specification is this “css-selectors” by Christer Sandberg:

https://github.com/chrsan/css-selectors

The next version of jOOX will include css-selector’s parser for simpler DOM navigation. The following two expressions will hold the same result:

Match match1 = $(document).find("book:empty");
Match match2 = $(document).xpath("//book[not(*|@*|node())]");

 

From http://lukaseder.wordpress.com/2012/01/26/css-selectors-in-java/

Tags:
Published at DZone with permission of Lukas Eder, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Aakash Murari replied on Sat, 2012/02/04 - 6:18am

I've used Jsoup (http://jsoup.org/) to parse real world HTML files by css selectors. It works really great.

 

 

Lukas Eder replied on Sat, 2012/02/04 - 11:59am in response to: AakashM

@Aakash: Yes, I've gotten some inspiration from jsoup as well. The main difference is that jsoup is not compatible to org.w3c.dom. jOOX is aimed for those users who need such compatibility

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.