javaandroidxmlsimple-frameworkamazon-advertising-api

Multiple elements with the same name with SimpleXML and Java


I'm trying to use SimpleXML to parse an XML document (an ItemLookupResponse for a book from the Amazon Product Advertising API) which contains the following element:

<ItemAttributes>
  <Author>Shane Conder</Author>
  <Author>Lauren Darcey</Author>
  <Manufacturer>Pearson Educacion</Manufacturer>
  <ProductGroup>Book</ProductGroup>
  <Title>Android Wireless Application Development: Barnes & Noble Special Edition</Title>
</ItemAttributes>

My problem is that I don't know how to deal with the multiple possible Author elements.

Here's what I have right now for the corresponding POJO (Plain Old Java Object), keeping in mind that it's not handling the case of multiple Authors:

@Element
public class ItemAttributes {
    @Element
    public String Author;

    @Element
    public String Manufacturer;

    @Element
    public String Title;
}

(I don't care about the ProductGroup, so it's not in the class -- I'm just setting SimpleXML's strict mode to off to allow for that.)

I couldn't find an example in the documentation that corresponded with such a case. Using an ElementList with (inline=true) seemed along the right lines, but I didn't see how to do it for String (as opposed to a separate Author class, which I have no need for and don't see how it would even work).

Here's a similar question and answer, but for PHP: php - simpleXML how to access a specific element with the same name as others? I don't know what the Java equivalent would be to the accepted answer.

Thanks in advance.


Solution

  • N.B, this is just from reading the docs (the only question is if it should be name="Author" or entry="Author"):

    @Element
    public class ItemAttributes {
        @Element(inline=true, type=String.class, name="Author")
        public List<String> authors;
    ...
    }