Google's Atom XML implementation (see detailed package specification).


Class Summary
AtomPatchContent Serializes Atom XML PATCH HTTP content based on the data key/value mapping object for an Atom entry.
AtomPatchRelativeToOriginalContent Serializes an optimal Atom XML PATCH HTTP content based on the data key/value mapping object for an Atom entry, by comparing the original value to the patched value.
GData Utilities for working with the Atom XML of Google Data API's.
MultiKindFeedParser<T> GData Atom feed parser when the entry class can be computed from the kind.

Package Description

Google's Atom XML implementation (see detailed package specification).

Package Specification

User-defined Partial XML data models allow you to defined Plain Old Java Objects (POJO's) to define how the library should parse/serialize XML. Each field that should be included must have an @Key annotation. The field can be of any visibility (private, package private, protected, or public) and must not be static.

The optional value parameter of this @Key annotation specifies the XPath name to use to represent the field. For example, an XML attribute a has an XPath name of @a, an XML element <a> has an XPath name of a, and an XML text content has an XPath name of text(). These are named based on their usage with the partial response/update syntax for Google API's. If the @Key annotation is missing, the default is to use the Atom XML namespace and the Java field's name as the local XML name. By default, the field name is used as the JSON key. Any unrecognized XML is normally simply ignored and not stored. If the ability to store unknown keys is important, use GenericXml.

Let's take a look at a typical partial Atom XML album feed from the Picasa Web Albums Data API:

<?xml version='1.0' encoding='utf-8'?>
<feed xmlns=''
  <link rel=''
    href='' />
  <entry gd:etag='"RXY8fjVSLyp7ImA9WxVVGE8KQAE."'>
    <category scheme=''
      term='' />
    <summary>Hilarious Felines</summary>

Here's one possible way to design the Java data classes for this (each class in its own Java file):

import java.util.List;

  public class Link {

    public String href;

    public String rel;

    public static String find(List<Link> links, String rel) {
      if (links != null) {
        for (Link link : links) {
          if (rel.equals(link.rel)) {
            return link.href;
      return null;

  public class Category {

    public String scheme;

    public String term;

    public static Category newKind(String kind) {
      Category category = new Category();
      category.scheme = "";
      category.term = "" + kind;
      return category;

  public class AlbumEntry {

    public String summary;

    public String title;

    public String access;

    public Category category = newKind("album");
    private String getEditLink() {
      return Link.find(links, "edit");

  public class Author {

    public String name;

  public class AlbumFeed {

    public Author author;

    public int totalResults;

    public List<AlbumEntry> photos;

    public List<Link> links;

    private String getPostLink() {
      return Link.find(links, "");

You can also use the @Key annotation to defined query parameters for a URL. For example:

public class PicasaUrl extends GoogleUrl {

  public Integer maxResults;

  public String kinds;

  public PicasaUrl(String url) {

  public static PicasaUrl fromRelativePath(String relativePath) {
    PicasaUrl result = new PicasaUrl(PicasaWebAlbums.ROOT_URL);
    result.path += relativePath;
    return result;

To work with a Google API, you first need to set up the GoogleTransport. For example:

  private static GoogleTransport setUpGoogleTransport() throws IOException {
    GoogleTransport transport = new GoogleTransport();
    transport.applicationName = "google-picasaatomsample-1.0";
    AtomParser parser = new AtomParser();
    parser.namespaceDictionary = PicasaWebAlbumsAtom.NAMESPACE_DICTIONARY;
    // insert authentication code...
    return transport;

Now that we have a transport, we can execute a partial GET request to the Picasa Web Albums API and parse the result:

  public static AlbumFeed executeGet(GoogleTransport transport, PicasaUrl url)
      throws IOException {
    url.fields = GData.getFieldsFor(AlbumFeed.class);
    url.kinds = "photo";
    url.maxResults = 5;
    HttpRequest request = transport.buildGetRequest();
    request.url = url;
    return request.execute().parseAs(AlbumFeed.class);

If the server responds with an error the HttpRequest.execute() method will throw an HttpResponseException, which has an HttpResponse field which can be parsed the same way as a success response inside of a catch block. For example:

    try {
    } catch (HttpResponseException e) {
      if (e.response.getParser() != null) {
        Error error = e.response.parseAs(Error.class);
        // process error response
      } else {
        String errorContentString = e.response.parseAsString();
        // process error response as string
      throw e;

To update an album, we use the transport to execute an efficient partial update request using the PATCH method to the Picasa Web Albums API:

  public AlbumEntry executePatchRelativeToOriginal(GoogleTransport transport,
      AlbumEntry original) throws IOException {
    HttpRequest request = transport.buildPatchRequest();
    request.headers.ifMatch = etag;
    PatchRelativeToOriginalContent content =
        new PatchRelativeToOriginalContent();
    content.namespaceDictionary = PicasaWebAlbumsAtom.NAMESPACE_DICTIONARY;
    content.originalEntry = original;
    content.patchedEntry = this;
    request.content = content;
    return request.execute().parseAs(AlbumEntry.class);

  private static AlbumEntry updateTitle(GoogleTransport transport,
      AlbumEntry album) throws IOException {
    AlbumEntry patched = album.clone();
    patched.title = "An alternate title";
    return patched.executePatchRelativeToOriginal(transport, album);

To insert an album, we use the transport to execute a POST request to the Picasa Web Albums API:

  public AlbumEntry insertAlbum(GoogleTransport transport, AlbumEntry entry)
      throws IOException {
    HttpRequest request = transport.buildPostRequest();
    AtomContent content = new AtomContent();
    content.namespaceDictionary = PicasaWebAlbumsAtom.NAMESPACE_DICTIONARY;
    content.entry = entry;
    request.content = content;
    return request.execute().parseAs(AlbumEntry.class);

To delete an album, we use the transport to execute a DELETE request to the Picasa Web Albums API:

  public void executeDelete(GoogleTransport transport) throws IOException {
    HttpRequest request = transport.buildDeleteRequest();
    request.headers.ifMatch = etag;

NOTE: As you might guess, the library uses reflection to populate the user-defined data model. It's not quite as fast as writing the wire format parsing code yourself can potentially be, but it's a lot easier.

NOTE: If you prefer to use your favorite XML parsing library instead (there are many of them), that's supported as well. Just call HttpRequest.execute() and parse the returned byte stream.

This package depends on theses packages:

Warning: this package is experimental, and its content may be changed in incompatible ways or possibly entirely removed in a future version of the library


Copyright © 2010 Google. All Rights Reserved.