As a developer I want to use XML extremely easily

A few days ago I needed to parse some reports in Java application. Recently I used for such tasks built-in features, but I can say they are not very user-friendly or intuitive. And I think that there's no reason to use something strange for implementing such simple things. I'll be much better to find very cool replacement. After spending some time googling I've checked that unfortunately there were nothing satisfactory. I've even started thinking about using Scala but luckily in last-ditch attempt I've found JOOX. Really weird name, but I've found out it's abbreviation from Java Object Oriented XML... hmmm - sounds promising :) Authors described it as "simple wrapper for the org.w3c.dom package, to allow for fluent XML document creation and manipulation where DOM is required but too verbose". For me it's enough to start looking for maven artifact:
Core element is the org.joox.JOOX class with static $ method (yes, yes - jQuery clutches) returning org.joox.Match interface - another very important element.

Let's start - method Match $(File file) seems to be good entry point. Now I need file for testing - the nearest one is my pom.xml - why not? :)
public class JooxSandbox {
  public static void main(String[] args) 
      throws IOException, SAXException {
    Match document = $(new File("pom.xml"));

Without any exceptions - great :) Let's move on and get first node!
  System.out.println("JOOX rocks!");
  // get tag name
  // get tag attribute
Compile, run and there's an output:
JOOX rocks!

Cool - another battle won. Now something more complex - iterating over dependencies should be ok.
Match dependencies = document.find("dependency");
System.out.println("Found dependencies:" + dependencies.size());
for (Match dependency : dependencies.each()) {
Dependencies count: 2

API is in fact quite easy, and if you prefer all the time exposing original elements (like org.w3c.dom.Element by calling get() method instead of the each()). In addition to finding elements we can perform filtering. For example if we want to retrive just even dependencies:
// even() is static method from JOOX class
But so far we're just reading existing content. Why stop there? I've got such project metadata:
We can change it by typing:
  .after("\n\tMy Sandbox");
And voila:
  My Sandbox

Notice that JOOX is operating on independent document, and if you want to save results you've to manually flush document.content() into file (or maybe I've overlooked something?). Anyway, I hope you'll enjoy it! For me it's the only one for parsing and modifying on XML in Java :)
Some useful links:


Unknown said…
just as it should be :)
Yogesh said…
I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.
ecommerce website development in chennai
william james said…
Looking for a nursing assignment make your visit at We will help you to get A+ Grades in assignments. At essaycorp, we provide exceptional nursing assignment writing help across the world at very affordable prices.
Nursing Assignment Help
If you are working on a big project you are facing lots of programming issues then there is a very good option to get Fiverr Freelance Professional Services and get your job done instantly

Popular posts from this blog

Using jstat to report custom JVM metric sets

Understanding Spring Web Initialization

Including Java agent in standalone Spring Boot application