I'm the one who just started using JAXB, all I need to do is write an object in xml and read it in java at some point
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 create the XML file
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 an error like:
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 donβt understand JABX and all its methods to the full, so itβs hard for me to understand, I was wrong, I tried to work a little and found that many people get this error, but still itβs hard to understand the problem, the solution is here ..
source share