简体   繁体   English

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

[英]Unable to get attribute value using JAXB ( For attribute in Parent class)

I am trying to read one xml using JAXB.我正在尝试使用 JAXB 读取一个 xml。

I am facing one weird issue where attribute of parent is not being read,but attributes of child classes are read.我面临一个奇怪的问题,即没有读取父类的属性,但读取了子类的属性。 I have referenced forums, but this seems to be a strange one.我参考了论坛,但这似乎是一个奇怪的论坛。

Can anyone please let me know what is the mistake i am doing.任何人都可以让我知道我在做什么错误。

XML. XML。

<?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>

PhoneDirectory Class电话目录类

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;
    }

}

PhoneNumber Class电话号码类

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;
    }


}

Main class主班

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());
    }

    }

}

Output输出

Get all phone details    
Exchange: Ashfield    
****Get exchangeName :null****    
23    
0489524401    
Test    
Test    
#34,rt road, State,Country,22344  

As you can see the exchangeName is null despite mentioning XMLAttribute annotation for field.正如您所看到的,尽管提到了字段的 XMLAttribute 注释,但 exchangeName 为空。

Thanks, Vishnu谢谢,毗湿奴

You've declared the exchangeName attribute in the PhoneDirectory class but your XML has this attribute in the Exchange element.您已在PhoneDirectory类中声明了exchangeName属性,但您的 XML 在Exchange元素中具有此属性。

So instead of所以代替

private String exchange;
private String exchangeName;

you'll need a class like Exchange with @XmlAttribute exchangeName and @XmlValue exchange .您将需要一个类,例如Exchange@XmlAttribute exchangeName@XmlValue exchange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM