我现在有两节课。 CustomerCustomerItem,每个Customer可以包含多个CustomerItems

@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer implements Serializable {

  private final String customerNumber;
  private final String customerName;

    @XmlElementWrapper(name = "customerItems")
    @XmlElement(name = "CustomerItem")
    private List<CustomerItem> customerItems;
...


通过REST,我们可以得到一个List<Customer>,这将导致XML如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
 <collection>
  <Customer>
    // normal values
    <customerItems>
      <customerItem>
        // normal values
      </customerItem>
    </customerItems>
  </Customer>
  <Customer>
    ...
  </Customer>
  <Customer>
    ...
  </Customer>
</collection>


现在,如果我想对响应进行编组,则会收到错误消息:


  javax.xml.bind.UnmarshalException:意外元素(uri:“”,
  当地:“收藏”)。预期元素是
  ,


private List<Customer> getCustomersFromResponse(HttpResponse response)
    throws JAXBException, IllegalStateException, IOException {

    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    InputStream content = response.getEntity().getContent();
    InputStreamReader reader = new InputStreamReader(content);

    java.util.List<Customer> unmarshal = (List<Customer>) jaxbUnmarshaller.unmarshal(reader);
    return unmarshal;

}


现在我知道了为什么它不起作用,显然Jaxb希望Customer是根元素,但是现在找到了一个集合(当返回List时,这似乎是默认值?)。

一种解决方法是创建一个新的Customer类,该类包含一个客户列表并使其成为根元素,但实际上我不希望有一个新类。

必须有一种方法告诉jaxb,我有要获取的类的列表,并且他应该查看collection标记之类的东西?

最佳答案

我在这里看到两种方式。

1)使用@XmlRootElement(name = "collection")创建特殊的包装类,并将其设置为反编组。

2)另一种方法-使用简单的xml解析器实现将输入SAX拆分为较小的输入,然后使用JAXB分别解析每个片段(请参见:Split 1GB Xml file using Java)。

我认为您不能简单地告诉JAXB:将此xml解析为类型为XXX的元素集。

07-24 18:36