我是一个刚刚开始使用JAXB的人,我所需要的就是将一个对象写入xml,然后在某个时刻将其读回java
下面是我的类:
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
}
}下面是我用来构造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();
}
}尽管我尝试运行WHen代码,但得到的错误信息如下:
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我还不完全理解JABX和它的所有方法,所以我很难理解这个错误,我试着用谷歌搜索了一下,发现很多人都理解了这个错误,但仍然很难理解这里的问题和解决方案。
发布于 2014-05-07 22:31:55
如果您的类没有使用@XmlRootElement进行注释,那么您需要将其包装在JAXBElement的一个实例中,就像您对marshal操作所做的那样:
jaxbMarshaller.marshal(new JAXBElement<VSM>(new QName("uri","local"), VSM.class, vsm), System.out);异常发生在您尝试封送VSM实例时,而不是这样做:
jaxbMarshaller.marshal(vsm, System.out);更新
感谢您的回答,这样做只会让我创建空的xml文件,但是现在我知道我做错了什么,我试图编写一个xml而不指定注释是愚蠢的。
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)感兴趣。
https://stackoverflow.com/questions/23519970
复制相似问题