Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get the error as "missing an @XmlRootElement annotation" while trying to marshall an object into xml file using JAXB

I am someone who has just started to use JAXB,All I need it for is write an object into xml and read it back into 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 construct 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 the error as :

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 dont understand JABX and all it methods fully yet ,so its kind of difficult for me understand this error,I tried googling a little and found a lot of people get this error, but still find it difficult to understand the problem the solution here..

like image 415
Snedden27 Avatar asked May 07 '14 14:05

Snedden27


1 Answers

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

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

The exception is coming from the time you try to marshal the instance of VSM wihtout doing that:

jaxbMarshaller.marshal(vsm, System.out);

UPDATE

Thanks for ur answer,doing that only let me create empty xml file, but now I know what I was doing wrong, it was stupid of me trying to write an xml without specifying the annoations

JAXB (JSR-222) implementations don't require any annotations (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html). The most common annotation added is @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 accessor methods then you may also be interested in the @XmlAccessorType(XmlAccessType.FIELD) annotation (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

like image 112
bdoughan Avatar answered Nov 12 '22 07:11

bdoughan