Java 我在尝试使用 JAXB 将对象编组到 xml 文件时收到错误消息“缺少 @XmlRootElement 注释”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23519970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I get the error as "missing an @XmlRootElement annotation" while trying to marshall an object into xml file using JAXB
提问by Snedden27
I am someone who has just started to use JAXB,All I need it for is write an object into xml and read it back into java at some point
我是刚开始使用 JAXB 的人,我需要的只是将一个对象写入 xml 并在某个时候将其读回 java
Here is my class:
这是我的课:
public class VSM implements java.io.Externalizable
{
ArrayList<String> termList; //Term Dictionary
ArrayList<String> queryTermList; //Query list
ArrayList<ArrayList<Doc>> docLists;
ArrayList<ArrayList<Doc>> queryDocLists;
double[] docLength; //Denominator for doc linearization
double queryLength; //Denominator for query lineriazation
HashMap<String, Double> queryDocLenght; //Vector for holding noramiliase queries
HashMap<String, Double> queryDoc;
String Docs[]; //List of file names
Double scoreCap=0.04; //Score cap to reduce the effect of stop words
public static String fileName = "indexedFiles.txt";
private static final long serialVersionUID = 7863262235394607247L;
public VSM()
{
//Some constructor code
}
}
Here is the method I use to construct the XML file
这是我用来构造 XML 文件的方法
public void writeXML(VSM vsm)
{
try {
File file = new File("IndexXmlfile.xml");
//JAXBElement<VSM> jaxbWrappedHeader = objectFactory.createHeader(obj);
JAXBContext jaxbContext = JAXBContext.newInstance(VSM.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(new JAXBElement<VSM>(new QName("uri","local"), VSM.class, vsm), System.out);
jaxbMarshaller.marshal(vsm, file);
jaxbMarshaller.marshal(vsm, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
Altough WHen I try to run the code I get the error as :
尽管当我尝试运行代码时,我得到的错误是:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "KPT.VSM" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at KPT.VSM.writeXML(VSM.java:477)
at KPT.VSM.main(VSM.java:511)
Caused by: com.sun.istack.SAXException2: unable to marshal type "KPT.VSM" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:249)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:339)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
... 4 more
I dont understand JABX and all it methods fully yet ,so its kind of difficult for me understand this error,I tried googling a little and found a lot of people get this error, but still find it difficult to understand the problem the solution here..
我还没有完全理解 JABX 及其所有方法,所以对我来说理解这个错误有点困难,我试着用谷歌搜索了一下,发现很多人都得到这个错误,但仍然很难理解这里的问题解决方案。 .
采纳答案by bdoughan
When your class is not annotated with @XmlRootElement
then you need to wrap it in an instance of JAXBElement
as you have done for one of your marshal
operations:
当您的类没有注释时,@XmlRootElement
您需要将它包装在一个实例中,JAXBElement
就像您为其中一个marshal
操作所做的那样:
jaxbMarshaller.marshal(new JAXBElement<VSM>(new QName("uri","local"), VSM.class, vsm), System.out);
The exception is coming from the time you try to marshal the instance of VSM
wihtout doing that:
异常来自您尝试编组不VSM
这样做的实例的时间:
jaxbMarshaller.marshal(vsm, System.out);
UPDATE
更新
Thanks for ur answer,doing that only let me create empty xml file, but now I know what I was doing wrong, it was stupid of me trying to write an xml without specifying the annoations
感谢您的回答,这样做只会让我创建空的 xml 文件,但现在我知道我做错了什么,我试图在不指定注释的情况下编写 xml 是愚蠢的
JAXB (JSR-222)implementations don't require any annotations (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html). The most common annotation added is @XmlRootElement
, but without it you can use JAXBElement
(see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html). If your class does not have public accessor methods then you may also be interested in the @XmlAccessorType(XmlAccessType.FIELD)
annotation (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
JAXB (JSR-222)实现不需要任何注释(请参阅:http: //blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html)。添加的最常见的注释是@XmlRootElement
,但没有它您可以使用JAXBElement
(参见:http: //blog.bdoughan.com/2012/07/jaxb-and-root-elements.html)。如果您的类没有公共访问器方法,那么您可能也对@XmlAccessorType(XmlAccessType.FIELD)
注释感兴趣(请参阅:http: //blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。