我是刚刚开始使用JAXB的人,所有我需要的是将对象写入xml并在某些时候将其读回
java
这是我的班级:
public class VSM implements java.io.Externalizable
{
ArrayList termList; //Term Dictionary
ArrayList queryTermList; //Query list
ArrayList> docLists;
ArrayList> queryDocLists;
double[] docLength; //Denominator for doc linearization
double queryLength; //Denominator for query lineriazation
HashMap queryDocLenght; //Vector for holding noramiliase queries
HashMap 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 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(new QName("uri","local"), VSM.class, vsm), System.out);
jaxbMarshaller.marshal(vsm, file);
jaxbMarshaller.marshal(vsm, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
Altough当我尝试运行代码时,我得到错误:
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及其所有方法,所以我很难理解这个错误,我尝试了一下谷歌搜索,发现很多人都得到了这个错误,但仍然觉得很难理解这里的问题. .