Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Build a simple AtomPub Java client to convert ATOM XML to JSON

DZone's Guide to

Build a simple AtomPub Java client to convert ATOM XML to JSON

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

In a previous post I showed you how to build a simple AtomPub client with Netbeans 7, Maven, Java and Apache Abdera. If you haven’t read that article then please do so now so that this new article makes more sense. Today I’m going to take the code from my previous article and modify it slightly using the Apache Abdera Extensions (in particular the JSON one) to convert the ATOM XML into JSON.

Grab the source code from the previous article and load it into Netbeans 7. Open the project’s POM file and add in the dependency for the Apache Abdera extensions:

<dependency>
  <groupId>org.apache.abdera</groupId>
  <artifactId>abdera-extensions-json</artifactId>
  <version>1.1.2</version>
</dependency>

The lines of code needed to use the JSON extension are simple, we will add the following:

// JSON Output
Writer writer = abdera.getWriterFactory().getWriter("json");
try {
    writer.writeTo(docFeed.getRoot(), System.out);
} catch(Exception e) {
    System.out.println(e.getMessage());
}

Go into the App.java file and modify it to look like this:

/*
 * Original code created by the Apache Abdera team
 * http://abdera.apache.org/
 */
package com.giantflyingsaucer.atompubclient;
 
import java.util.Date;
 
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.abdera.writer.Writer;
 
public class App {
 
    public static void main(String[] args) throws Exception {
 
        Abdera abdera = new Abdera();
        AbderaClient abderaClient = new AbderaClient(abdera);
        Factory factory = abdera.getFactory();
 
        Entry entry = factory.newEntry();
        entry.setId("tag:example.org,2011:foo");
        entry.setTitle("This is the title");
        entry.setUpdated(new Date());
        entry.addAuthor("Chad");
        entry.setContent("Hello World");
        report("The Entry to Post", entry.toString());
 
        Document<Entry> doc = abderaClient.post("http://localhost:9002/employee", entry).getDocument();
 
        report("The Created Entry", doc.getRoot().toString());
 
        ClientResponse resp = abderaClient.get("http://localhost:9002/employee");
        if (resp.getType() == ResponseType.SUCCESS) {
            Document<Feed> docFeed = resp.getDocument();
            // JSON Output
            Writer writer = abdera.getWriterFactory().getWriter("json");
            try {
                writer.writeTo(docFeed.getRoot(), System.out);
            } catch(Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
          // Error
        }
    }
 
    private static void report(String title, String message) {
        System.out.println("== " + title + " ==");
        if (message != null)
            System.out.println(message);
        System.out.println();
    }
}

That’s all there is to it. Go ahead and use the code from my other article where I showed you how to build an ATOMPub server. Start the ATOMPub server and then run this modified client app and you’ll see your results in JSON.

Results:

{
 "id":"tag:acme.com,2007:employee:feed",
 "title":"Acme Employee Database",
 "updated":"2011-09-15T15:38:38.260Z",
 "authors":[{
   "name":"Acme Industries"
  }
 ],
 "links":[{
   "href":"http://localhost:9002/employee"
  },{
   "href":"http://localhost:9002/employee",
   "rel":"self"
  }
 ],
 "entries":[{
   "id":"tag:giantflyingsaucer.com,2011:employee:entry:1000",
   "title":"Hello World",
   "content":"Hello World",
   "authors":[{
     "name":"Acme Industries"
    }
   ],
   "links":[{
     "href":"http://localhost:9002/employee/1000-Hello_World",
     "rel":"edit"
    }
   ]
  }
 ]
}

From http://www.giantflyingsaucer.com/blog/?p=3086

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}