vsm java_java – 我尝试使用JAXB将对象编组到xml文件中时,错误为“缺少@XmlRootElement注释”...

我是刚刚开始使用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及其所有方法,所以我很难理解这个错误,我尝试了一下谷歌搜索,发现很多人都得到了这个错误,但仍然觉得很难理解这里的问题. .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值