1

我有从 REST 返回的 xml,它如下所示:

<customers>
    <customer>
        <addressline1>111 E. Las Olas Blvd</addressline1>
        <addressline2>Suite 51</addressline2>
        <city>Fort Lauderdale</city>
        <creditLimit>100000</creditLimit>
        <customerId>1</customerId>
        <discountCode>
            <discountCode>56</discountCode>
            <rate>0.00</rate>
        </discountCode>
        <email>jumbocom@gmail.com</email>
        <fax>305-777-4635</fax>
        <name>Kupa</name>
        <phone>305-777-4632</phone>
        <state>FL</state>
        <zip>
            <areaLength>22.0</areaLength>
            <areaWidth>23.0</areaWidth>
            <radius>50.0</radius>
            <zipCode>90-200</zipCode>
        </zip>
    </customer>
</customers>

我正在尝试解组列表,但列表始终为空。也许原因是在 Customer 类中,Zip 和 DiscountCode 是其他实体,所以我只有 XML 作为字符串,它不能从中创建实体。

这些是我的java类:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customers 
{
   @XmlElement
   private List<Customer> customers = new ArrayList<Customer>();

   public List<Customer> getCustomers()
   {
      return customers;
   }
} 


@Entity
@Table(name = "customer")
@XmlRootElement(name = "customer")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "customer_id")
    private Integer customerId;
    @Column(name = "name")
    private String name;
    @Column(name = "addressline1")
    private String addressline1;
    @Column(name = "addressline2")
    private String addressline2;
    @Column(name = "city")
    private String city;
    @Column(name = "state")
    private String state;
    @Column(name = "phone")
    private String phone;
    @Column(name = "fax")
    private String fax;
    @Column(name = "email")
    private String email;
    @Column(name = "credit_limit")
    private Integer creditLimit;
    @JoinColumn(name = "discount_code", referencedColumnName = "discount_code")
    @ManyToOne(optional = false)
    private DiscountCode discountCode;
    @JoinColumn(name = "zip", referencedColumnName = "zip_code")
    @ManyToOne(optional = false)
    private MicroMarket zip;

// later only setters and getters
}

这就是我解组的方式:

JAXBContext context = JAXBContext.newInstance(Customers.class, Customer.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Customers customers = (Customers) unmarshaller.unmarshal(new StringReader(allXmlString));

我见过类似的主题,但其中任何一个都对我有帮助。为什么解组后我的列表总是空的?

感谢帮助。

4

1 回答 1

3

customers字段的默认映射将查找名为customers. 在@XmlElement注释上,您需要指定要映射到名为customer.

@XmlElement(name="customer")
private List<Customer> customers = new ArrayList<Customer>();

顾客

下面是整个Customers班级的样子:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customers 
{
   @XmlElement(name="customer")
   private List<Customer> customers = new ArrayList<Customer>();

   public List<Customer> getCustomers()
   {
      return customers;
   }
} 

了解更多信息

于 2012-08-24T15:48:43.903 回答