JAXB解组 - Java字段

时间:2016-11-21 17:46:35

标签: java jaxb

我正在尝试使用JAXB解组将以下XML转换为Java类,但我找不到将“zipCode”等XML属性转换为java字段“zipCode”的方法。我怎样才能在JAXB中实现这一目标?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<datacollection id="amazonOrder">
    <table name="AmazonOrderTable">
        <row>
            <field name="zipCode">20170</field>
            <field name="customerLastName">DE</field>
            <field name="state">VA</field>            
            <field name="city">COMMERCE CITY</field>
            <field name="serialNumber">818243CX601252Y</field>
            <field name="homePhone">3032885239</field>
            <field name="customerFirstName">ROGER</field>
            <field name="customerAddress">13390 E 106TH PL</field>
        </row>
    </table>
</datacollection>
public class CustomerInfo {

protected String customerAddress;
protected String customerFirstName;
protected String customerLastName;
protected String serialNumber;
protected String zipCode;    
protected String city;

public String getCustomerAddress() {
    return customerAddress;
}

public void setCustomerAddress(String customerAddress) {
    this.customerAddress = customerAddress;
}

public String getCustomerFirstName() {
    return customerFirstName;
}

public void setCustomerFirstName(String customerFirstName) {
    this.customerFirstName = customerFirstName;
}

public String getCustomerLastName() {
    return customerLastName;
}

public void setCustomerLastName(String customerLastName) {
    this.customerLastName = customerLastName;
}

public String getSerialNumber() {
    return serialNumber;
}

public void setSerialNumber(String serialNumber) {
    this.serialNumber = serialNumber;
}

public String getZipCode() {
    return zipCode;
}

public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}
}

2 个答案:

答案 0 :(得分:1)

您可以尝试将@XmlElement(name = "zipcode")注释添加到getZipCode()setZipCode()方法中。

答案 1 :(得分:0)

在你的例子中,&#34; zipCode&#34;不是属性。这是&#34; name&#34;的价值。属性。生成JAXB类时,JAXB会将元素和属性转换为字段,但这些值将分配给字段,而不是用于命名字段。