简体   繁体   English

尝试使用JAXB将对象编组为xml文件时,出现“缺少@XmlRootElement批注”错误

[英]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 我是刚开始使用JAXB的人,我需要做的就是将对象写入xml并在某个时候将其读回Java

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 这是我用来构造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 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.. 我还不完全了解JABX及其所有方法,因此它对我来说很难理解此错误,我尝试了一下Google搜寻,发现很多人都遇到了这个错误,但是仍然很难在这里理解问题的解决方案。 。

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: 当你的类不带注释@XmlRootElement那么你需要用它的一个实例JAXBElement为你做你的一个marshal操作:

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: 例外是您尝试封送VSM实例的时间而来的:

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 谢谢你的回答,这样做只会让我创建一个空的xml文件,但是现在我知道自己做错了,这很愚蠢,因为我试图在不指定注释的情况下编写xml

JAXB (JSR-222) implementations don't require any annotations (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html ). JAXB(JSR-222)实现不需要任何注释(请参阅: 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 ). 添加的最常见的注释是@XmlRootElement ,但是如果没有它,则可以使用JAXBElement (请参阅: http : @XmlRootElement )。 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 ). 如果您的类没有公共访问器方法,那么您可能也对@XmlAccessorType(XmlAccessType.FIELD)注释感兴趣(请参阅: http : @XmlAccessorType(XmlAccessType.FIELD) 。 html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM