DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. As a developer I want to use XML extremely easily

As a developer I want to use XML extremely easily

Jakub Kubrynski user avatar by
Jakub Kubrynski
·
Mar. 11, 13 · Interview
Like (0)
Save
Tweet
Share
2.90K Views

Join the DZone community and get the full member experience.

Join For Free
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:
<dependency>
  <groupid>org.jooq</groupid>
  <artifactid>joox</artifactid>
  <version>1.1.0</version>
</dependency>
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
  System.out.println(document.tag());
  // get tag attribute
  System.out.println(document.attr("xmlns")); 
Compile, run and there's an output:
JOOX rocks!
project
http://maven.apache.org/POM/4.0.0

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()) {
  System.out.println(Joiner.on(':').join(
      dependency.find("groupId").content(), 
      dependency.find("artifactId").content(), 
      dependency.find("version").content()));
}
produces
Dependencies count: 2
com.google.guava:guava:13.0
org.jooq:joox:1.1.0

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
dependencies.filter(even()).each(); 
But so far we're just reading existing content. Why stop there? I've got such project metadata:
<groupid>pl.kuba.sandbox</groupid>
<artifactid>sandbox</artifactid>
<version>1.0</version>
We can change it by typing:
document.xpath("//version")
  .content("1.0.1-SNAPSHOT")
  .after("\n\tMy Sandbox");
System.out.println(document.content());
And voila:
  <groupid>pl.kuba.sandbox</groupid>
  <artifactid>sandbox</artifactid>
  <version>1.0.1-SNAPSHOT</version>
  <description>My Sandbox</description>

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:
http://code.google.com/p/joox/
https://github.com/jOOQ
XML

Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • A First Look at Neon
  • What Is API-First?
  • 10 Things to Know When Using SHACL With GraphDB

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: