// Find the order at index for and add an element "paid"
joox(document).find("orders")
.children()
.eq(4)
.append("<paid>true</paid>");
// Find those orders that are paid and flag them as "settled"
joox(document).find("orders")
.children()
.find("paid")
.after("<settled>true</settled>");
Another Fluent API: jOOX. Porting jQuery to Java
Recently, in my every day programming madness, I really felt the urge to kill someone involved with the formal specification of DOM. The beloved Document Object Model. While everyone understands that this API is complete in functionality and scope and it’s a standard, and it’s almost the same in every language…. well it’s incredibly verbose. Manipulating XML is about as fun and exciting as cleaning the dishes of a 2000-people Indian wedding.
And then, suddenly, I remembered that this is how I felt with Java’s support for advanced SQL and how JPA/CriteriaQuery made me feel like that poor dishwasher, before. And I wondered whether someone had felt like me before. So I asked this question on Stack Overflow:
https://stackoverflow.com/questions/6996013/a-nice-java-xml-dom-utility
And I got the expected answers about JDOM and dom4j. Two dinosaur projects that are neither sexier nor more efficient than the standard itself (e.g. Xerces). See this answer about performance:
https://stackoverflow.com/questions/6996013/a-nice-java-xml-dom-utility#6998870
I had also found one project, that has a somewhat fluent approach:
https://code.google.com/p/xmltool/
It looks quite nice, actually, although it is a bit biased towards DOM creation, not navigation. And then, it struck me like lightning: “Why hasn’t anyone ported jQuery” to Java, yet?? jQuery is exactly how an XML API should be: Awesome. Fluent. Fun, and efficient to use. So I tried to hack something together that looks like jQuery and that’s the beginning of another product in the “jOO-Star suite”: jOOX with X for XML! I wanted this to be fluent, and fun and efficient to use. Like jOOQ. So jOOX will be an attempt for doing precisely that. Here’s an example of what jOOX code looks like:
This rapid prototype of a jQuery port looks very promising to me, even if the most important features aren’t there yet (e.g. navigation with expression languages, selectors, etc). With Java’s static typing and without all the browser-related issues and JavaScript event handling and CSS and all that, pure DOM navigation and manipulation is actually not that hard to wrap. In any case, I have now even more respect for the jQuery guys, as I’m just touching the tip of the iceberg.
So in the future, I will also post one or two entries about jOOX on this blog. Looking forward to feedback!
Download jOOX from Google Code:
https://code.google.com/p/joox/
On the other hand, maybe I did in fact miss these projects? Interesting to see the similarities, differences:
Both frameworks operate on HTML though. jsoup rolls their own DOM model, which is not compliant to the standard org.w3c.dom package…
Maybe another project is worth visiting: https://jodd.org/doc/jerry/index.html
I know Jerry – I referenced it from the https://code.google.com/p/joox/ homepage.
Note that all of Jerry, JSoup, GWTQuery have their own internal DOM implementations. Yet, the standard DOM API and its reference implementation Xerces is one of the fastest and most memory-optimised implementations, and it is really about XML, not HTML, which allows for non-well-formed documents
Ahh, didn’t see this… Sorry. BTW, jOOX seems to be pretty cool!
Thanks! Well, the credit goes to jQuery, of course!