views:

17

answers:

1

Using the google-api-java-client I have gathered that the library parses the xml based on the classes you create and the keys you make. For example: if you have the following XML:

<entry test="ok">
<link name="somewhere.org"/>
</entry>

Then you could have these two classes:

public class Entry
{
  @Key("@test")
  public String test;

  @Key("link")
  public Link link;
}

public class Link
{
  @Key("name")
  public String name;
}

And the library would parse the xml and create the appropriate classes (if I understand it correctly)

If that is the case, how does one represent an xml tag that has both attributes and a value? Example:

<entry test="ok">
    <link name="somewhere.org">SomeValue</link>
</entry>

In particular I'm trying to represent a record such as the following so I can insert it into a google docs spreadsheet:

<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:gs="http://schemas.google.com/spreadsheets/2006"&gt;
  <title>Darcy</title>
  <gs:field name='Birthday'>2/10/1785</gs:field>
  <gs:field name='Age'>28</gs:field>
  <gs:field name='Name'>Darcy</gs:field>
  <gs:field name='CanVote'>No</gs:field>
</entry>

Also, where is this documented? I can't find the documentation but perhaps I'm just not looking in the right place.

A: 

The best documentation for the XML data model in the google-api-java-client library is the XML JavaDoc.

The @Key annotation to use with the name attribute is "@name". So you are only missing one character :)

public class Link
{
  @Key("@name")
  public String name;
}

See an example of the Link class in the calendar-v2-atom-oauth-sample.

Full disclosure: I'm an owner of the google-api-java-client project.

Yaniv Inbar
Ah, that documentation link was very helpful. If I understand it correctly then to get the attribute name you would use @key("@name") and then to get the textual value of the link tag, ('SomeValue' above) then you would use the syntax @Key("text()"). Is that correct? Thanks for the help btw!
BoredAndroidDeveloper
Right that's the idea. By the way: @Key String name is short for @Key("name") String name.
Yaniv Inbar