我是刚刚开始使用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();
}
}
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及其所有方法,所以对我来说很难理解这个错误,我尝试用谷歌搜索了一下,发现很多人都得到了这个错误,但仍然觉得很难理解这个问题解决方案在这里..
答案 0 :(得分:18)
如果您的课程未使用@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)。