2

我正在尝试使用 SimpleXML 解析包含以下元素的 XML 文档(来自Amazon Product Advertising APIItemLookupResponse的一本书):

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

我的问题是我不知道如何处理多个可能的Author元素。

这是我现在拥有的相应 POJO(普通旧 Java 对象)的内容,请记住它不处理多个Authors 的情况:

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

    @Element
    public String Manufacturer;

    @Element
    public String Title;
}

(我不关心ProductGroup,所以它不在课堂上——我只是将 SimpleXML 的strict模式设置off为允许这样做。)

我在与这种情况相对应的文档中找不到示例。使用ElementListwith(inline=true)似乎是正确的,但我没有看到如何为 String 做它(与单独的 Author 类相反,我不需要也看不到它是如何工作的)。

这是一个类似的问题和答案,但对于 PHP:php - simpleXML 如何访问与其他元素同名的特定元素?我不知道 Java 等效于接受的答案。

提前致谢。

4

1 回答 1

3

注意,这只是来自阅读文档(唯一的问题是它应该是 name="Author" 还是 entry="Author"):

@Element
public class ItemAttributes {
    @Element(inline=true, type=String.class, name="Author")
    public List<String> authors;
...
}
于 2011-08-14T21:29:24.080 回答