I get the "Missing @XmlRootElement Annotation" error when trying to organize an object into an XML file using JAXB

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 ..

+6
source share
1 answer

When your class is not annotated with @XmlRootElement , you need to wrap it in a JAXBElement instance, as was done for one of your marshal operations:

 jaxbMarshaller.marshal(new JAXBElement<VSM>(new QName("uri","local"), VSM.class, vsm), System.out); 

The exception comes from the moment you try to marshal the VSM instance with this:

 jaxbMarshaller.marshal(vsm, System.out); 

UPDATE

Thanks for ur answer, doing this, just let me create an empty XML file, but now I know what I was doing wrong, it was stupid with me, trying to write xml without specifying annotations

JAXB (JSR-222) implementations do not require annotations (see http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html ). The most common added annotation is added by @XmlRootElement , but without it you can use JAXBElement (see http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html ). If your class does not have public access methods, you may also be interested in the @XmlAccessorType(XmlAccessType.FIELD) annotation @XmlAccessorType(XmlAccessType.FIELD) (see http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html ).

+17
source

All Articles