无法使用JAXB获取属性值(对于Parent类中的属性)

时间:2016-11-22 00:54:42

标签: xml jaxb

我正在尝试使用JAXB读取一个xml。

我面临一个奇怪的问题,即没有读取父元素的属性,而是读取子类的属性。 我引用过论坛,但这似乎很奇怪。

任何人都可以让我知道我在做什么错误。

XML。

android-support-v7-appcompat

PhoneDirectory Class

<?xml version="1.0" encoding="UTF-8"?>
<PhoneDirectory>
<Exchange exchangeName="ashfield2133">Ashfield</Exchange>
<PhoneNumber id="23" number="0489524401">
<FirstName>Test</FirstName>
<LastName>Test</LastName>
<Address>#34,rt road, State,Country,22344 </Address>
</PhoneNumber>

<PhoneNumber id="88" number="0409545401">
<FirstName>Testf2</FirstName>
<LastName>Testl2</LastName>
<Address>St 2 , State,Country,34555</Address>
</PhoneNumber>


<PhoneNumber id="88" number="0446775401">
<FirstName>Testf3</FirstName>
<LastName>Testl3</LastName>
<Address>St 3 , State,Country,546777</Address>
</PhoneNumber>

</PhoneDirectory>

电话号码类

package com.test.phoneDirectory.dataclass;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;


@XmlRootElement(name="PhoneDirectory")
public class PhoneDirectory {

    private String exchange;
    private String exchangeName;

    @XmlElement(name="Exchange")
    public String getExchange() {
        return exchange;
    }

    public void setExchange(String exchange) {
        this.exchange = exchange;
    }

    @XmlAttribute(name="exchangeName")
    public String getExchangeName() {
        return exchangeName;
    }

    public void setExchangeName(String exchangename) {
        this.exchangeName = exchangename;
    }

    private List<PhoneNumber> phoneNumber;

    @XmlElement(name="PhoneNumber")
    public List<PhoneNumber> getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(List<PhoneNumber> phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

主要课程

package com.test.phoneDirectory.dataclass;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="PhoneNumber")
public class PhoneNumber {

    private String id;
    private String number;

    private String firstName;
    private String lastName;
    private String address;

    @XmlAttribute(name="id")
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @XmlAttribute(name="number")
    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @XmlElement(name="FirstName")
    public String getFirstName() {
        return firstName;
    }

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

    @XmlElement(name="LastName")
    public String getLastName() {
        return lastName;
    }

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

    @XmlElement(name="Address")
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}

输出

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.test.phoneDirectory.dataclass.PhoneDirectory;
import com.test.phoneDirectory.dataclass.PhoneNumber;


public class GetAllPhoneData {

    public static void main(String[] args) throws JAXBException {
        // TODO Auto-generated method stub
        JAXBContext jc = JAXBContext.newInstance(PhoneDirectory.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        PhoneDirectory phoneDirectory = (PhoneDirectory) unmarshaller.unmarshal(new File("src/config/PhoneDirectory.xml"));


        System.out.println("Get all phone details");
        System.out.println("Exchange:"+phoneDirectory.getExchange());
        System.out.println("exchangeName:"+phoneDirectory.getExchangeName());


      for (PhoneNumber phonedetails : phoneDirectory.getPhoneNumber()) {

          System.out.println(phonedetails.getId());
          System.out.println(phonedetails.getNumber());
          System.out.println(phonedetails.getFirstName());
          System.out.println(phonedetails.getLastName());
          System.out.println(phonedetails.getAddress());
    }

    }

}

正如您所见,尽管提到了字段的XMLAttribute注释,但exchangeName为null。

谢谢, 毗

1 个答案:

答案 0 :(得分:1)

您已在exchangeName类中声明了PhoneDirectory属性,但您的XML在Exchange元素中具有此属性。

所以而不是

private String exchange;
private String exchangeName;

您需要Exchange类,@XmlAttribute exchangeName@XmlValue exchange