How do I create classes that represent complex xml tag structures in google-api-java-client

StackOverflow https://stackoverflow.com/questions/3919034

  •  29-09-2019
  •  | 
  •  

Question

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">
  <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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top