Jaxb 2计数IllegalAnnotationExceptions

时间:2015-05-29 11:20:12

标签: java xml jaxb

我一直收到此错误,我的应用程序无法启动,我是否应该尝试{block?

解组

File file = new File("xmlFiles/ipAdresses.xml");

            JAXBContext jaxbContext = JAXBContext.newInstance(IpAdressListXmlHandler.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            IpAdressListXmlHandler xmlList = (IpAdressListXmlHandler) jaxbUnmarshaller.unmarshal(file);
            System.out.println(xmlList);

没有文件,我删除了

班级

@XmlRootElement
public class JAXBHandler {

    private String serverid;
    private String clientFileDir;
    private String serverFileDirectory;
    private int port;



    public void setPort(int port) {
        this.port = port;
    }


    public void setClientFileDir(String dir) {
        this.clientFileDir = dir;
    }




    public void setServerFileDirectory(String serverDir) {
        this.serverFileDirectory = serverDir;
    }


    public void setServerId(String serverId) {
        this.serverid = serverId;
    }


    @XmlElement
    public String getServerid() {
        return serverid;
    }
    @XmlElement
    public String getClientFileDir() {
        return clientFileDir;
    }
    @XmlElement
    public String getServerFileDirectory() {
        return serverFileDirectory;
    }
    @XmlElement
    public int getPort() {
        return port;
    }

我希望这是一件简单的事,整夜都在忙着(认真地)

1 个答案:

答案 0 :(得分:0)

是的,您需要在try {}块内部编写代码,并确保您的@XmlRootElement也存在于unmarshall中,这表明这是所有其他子类的基础对象类。

 File file = new File("xmlFiles/ipAdresses.xml");
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(IpAdressListXmlHandler.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        IpAdressListXmlHandler xmlList = (IpAdressListXmlHandler) jaxbUnmarshaller.unmarshal(file);
        System.out.println(xmlList);
    } catch (Exception ex) {
        Logger.getLogger(IpAdressListXmlHandler.class.getName()).log(Level.SEVERE, null, ex);
    }