Jaxb unmarshalling返回null和单个对象

时间:2017-05-02 15:33:20

标签: java xml jaxb

我正在尝试解组一个xml文件。这是我的文件。

  

XML文件

<AccountInfo>
   <Account>
    <FirstName>FirstName1</FirstName>
    <LastName>LastName1</LastName>
    <Country>US</Country>
    <PhoneNumber>2022023111</PhoneNumber>
    <Email>test@test.com</Email>
    <Password>1222433123</Password>
    <PaymentMethod>
        <CardNumber>4111111111111111</CardNumber>
        <Month>12</Month>
        <Year>2017</Year>
        <CVV>098</CVV>
        <StreetAddress>test</StreetAddress>
        <Town>test</Town>
        <State>Virginia</State>
        <ZipCode>12365</ZipCode>
        <Country>US</Country>
    </PaymentMethod>
 </Account>
 <Account>
    <FirstName>FirstName1</FirstName>
    <LastName>LastName1</LastName>
    <Country>US</Country>
    <PhoneNumber>2022023111</PhoneNumber>
    <Email>test@test.com</Email>
    <Password>1222433123</Password>
    <PaymentMethod>
        <CardNumber>4111111111111111</CardNumber>
        <Month>12</Month>
        <Year>2017</Year>
        <CVV>098</CVV>
        <StreetAddress>test</StreetAddress>
        <Town>test</Town>
        <State>Virginia</State>
        <ZipCode>12365</ZipCode>
        <Country>US</Country>
    </PaymentMethod>
 </Account>
</AccountInfo>
  

JaxbHelper.java

 public static <T> T unmarshal(InputStream file, Class<T> unmarshalClass) 
 throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(unmarshalClass);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    T object = (T) jaxbUnmarshaller.unmarshal(file);
    return object;
}
  

AccountInfo.java

  @XmlRootElement(name = "AccountInfo")
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlSeeAlso({PaymentMethod.class})
  public class AccountInfo {

  @XmlElement(name = "FirstName")
  private String FirstName;
  @XmlElement(name = "LastName")
  private String LastName;
  @XmlElement(name = "Country")
  private String Country;
  @XmlElement(name = "PhoneNumber")
  private String PhoneNumber;
  @XmlElement(name = "Email")
  private String Email;
  @XmlElement(name = "Password")
  private String Password;
  @XmlElement(name = "PaymentMethod")
  private PaymentMethod paymentMethod;

  public AccountInfo() {
    setFirstName(null);
    setLastName(null);
    setCountry(null);
    setPhoneNumber(null);
    setEmail(null);
    setPassword(null);
    setPaymentMethod(null);
}

public AccountInfo(String FirstName, String LastName, String Country, String PhoneNumber, String Email, String Password, PaymentMethod paymentMethod) {
    setFirstName(FirstName);
    setLastName(LastName);
    setCountry(Country);
    setPhoneNumber(PhoneNumber);
    setEmail(Email);
    setPassword(Password);
    setPaymentMethod(paymentMethod);
}


public String getFirstName() {
    return FirstName;
}

public void setFirstName(String firstName) {
    this.FirstName = firstName;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String lastName) {
    this.LastName = lastName;
}

public String getCountry() {
    return Country;
}

public void setCountry(String country) {
    this.Country = country;
}

public String getPhoneNumber() {
    return PhoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.PhoneNumber = phoneNumber;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    this.Email = email;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    this.Password = password;
}

public PaymentMethod getPaymentMethod() {
    return paymentMethod;
}


public void setPaymentMethod(PaymentMethod paymentMethod) {
    this.paymentMethod = paymentMethod;
  }
}

我如何消耗unmarshal

  List<T> accountInfo = (List<T>)ProcessClassHelper.unmarshal(xmlfile, Class.forName(Constants.ClassesDir +className));

我不确定导致问题的是什么,但即使有多个帐户且返回的数据为空,对象也会作为单个对象返回。

提前致谢。

1 个答案:

答案 0 :(得分:3)

您的Java类与XML文件的结构不匹配。 正如@Rhayene已经评论过的那样,你需要单独的Java类 AccountInfoAccount

AccountInfo将仅包含Account s的列表:

@XmlRootElement(name = "AccountInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class AccountInfo {

    @XmlElement(name = "Account")
    private List<Account> accounts;

    // + constructors, getters, setters
}

Account将包含您之前在AccountInfo中所拥有的所有属性 (顺便说一句:您还应该遵循以小写字母开头的字段名称约定。这不会破坏任何内容,因为XML名称仍由@XmlElement指定)

@XmlAccessorType(XmlAccessType.FIELD)
public class Account {

    @XmlElement(name = "FirstName")
    private String firstName;
    @XmlElement(name = "LastName")
    private String lastName;
    @XmlElement(name = "Country")
    private String country;
    @XmlElement(name = "PhoneNumber")
    private String phoneNumber;
    @XmlElement(name = "Email")
    private String email;
    @XmlElement(name = "Password")
    private String password;
    @XmlElement(name = "PaymentMethod")
    private PaymentMethod paymentMethod;

    // + constructors, getters, setters
}