Ich bekomme den Fehler "fehlt ein @XmlRootElement-annotation" beim Versuch, marshall ein Objekt in die xml-Datei mit JAXB

Ich bin jemand, der gerade begonnen hat zu verwenden, JAXB,Alles was ich brauche es für die schreiben, die ein Objekt ins xml-Format und Lesen Sie es wieder in java irgendwann

Hier ist meine Klasse:

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

Hier ist die Methode, die ich verwenden zum erstellen der XML-Datei

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();
                  }

      }

Obwohl Wenn ich versuche den code auszuführen bekomme ich die Fehlermeldung wie :

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

Ich verstehe nicht, JABX und alle Methoden vollständig noch ,so ist seine Art von schwer für mich zu verstehen, dieser Fehler,ich habe versucht, googeln Sie ein wenig und fand eine Menge Leute diese Fehlermeldung erhalten, aber immer noch finden es schwierig zu verstehen, das problem hier die Lösung..

InformationsquelleAutor Snedden27 | 2014-05-07
Schreibe einen Kommentar