Multiple elements with the same name with SimpleXML and Java

Posted by LouieGeetoo on Stack Overflow See other posts from Stack Overflow or by LouieGeetoo
Published on 2011-08-14T20:37:05Z Indexed on 2012/11/01 23:01 UTC
Read the original article Hit count: 303

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about android