我可以在主要方法JAVAXB,JAVAFX之外执行编组

时间:2016-10-01 06:52:34

标签: java xml javafx jaxb

我无法理解如何使用JAXB来保存XML文件的信息。我正在构建一个GUI,用于将用户的信息保存到XML文件中。我想让ListManagement类通过让另一个类调用marshal()方法来处理它,在该方法中它应该将对象添加到XML文件中。但事实并非如此。

我得到了1个IllegalAnnotation Exceptions

ListManagement类

import java.io.File;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;

public class ListManagement 

{

    File file = new File("src\\view\\DataFile.xml");    

    public ListManagement()
    {

    }

    public void marshal(Song track)
    {
        try
        {
            JAXBContext context = JAXBContext.newInstance(Song.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(track, file);
        }
        catch(Exception e)
        {
            System.out.println("ERROR" + e.getMessage());
        }

    }
}

歌曲课程

import javax.xml.bind.annotation.*;


@XmlRootElement

public class Song 
{   
    private String name;

    public Song(String name)
    {
        this.name = name;

    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }


    public String toString()
    {
        return this.name;
    }

}

1 个答案:

答案 0 :(得分:1)

如果您停止隐藏错误消息,您将会看到错误。

将您的catch声明更改为:

catch(Exception e)
{
    e.printStackTrace(System.out);
}

然后你会得到真正的错误:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Song does not have a no-arg default constructor.
    this problem is related to the following location:
        at Song

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at ListManagement.marshal(Test.java:18)
    at Test.main(Test.java:8)

这应该具体到足以让你明白。

收到错误时,始终查看完整堆栈跟踪