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. Build a simple AtomPub Java client to convert ATOM XML to JSON

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

Chad Lung user avatar by
Chad Lung
·
Oct. 22, 11 · Interview
Like (0)
Save
Tweet
Share
6.19K Views

Join the DZone community and get the full member experience.

Join For Free

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

JSON XML Build (game engine) Convert (command) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • REST vs. Messaging for Microservices
  • HTTP vs Messaging for Microservices Communications
  • Spring Boot, Quarkus, or Micronaut?

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: