class sabir.test.jms.entity.Message也不知道它的任何超类

时间:2017-07-02 14:37:51

标签: java xml jaxb

我正在关注this教程,我需要创建一个类,它具有将任何对象转换为xml的函数。

示例中的

,正在使用以下代码:

    import java.io.FileOutputStream;  

import javax.xml.bind.JAXBContext;  
import javax.xml.bind.Marshaller;  


public class ObjectToXml {  
public static void main(String[] args) throws Exception{  
    JAXBContext contextObj = JAXBContext.newInstance(Employee.class);  

    Marshaller marshallerObj = contextObj.createMarshaller();  
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  

    Employee emp1=new Employee(1,"Vimal Jaiswal",50000);  

    marshallerObj.marshal(emp1, new FileOutputStream("employee.xml"));  

}  
}  

这是我的代码:

package sabir.test.jms.services;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import sabir.test.jms.entity.Message;  



public class ObjectToXml {
    public static void convertObjectToXml (Object object) throws IOException, JAXBException
    {


    JAXBContext contextObj = JAXBContext.newInstance(object.ExCourse.class);  

    Marshaller marshallerObj = contextObj.createMarshaller();  
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  

    FileOutputStream xmlFile = new FileOutputStream(object.getClass().getName() + ".xml");

    marshallerObj.marshal(object, xmlFile);  

    } 

    public static void main (String[] args)
    {
        Message msg = new Message ("1","MT 103", "Sabir","Banda","CIMB","May Bank", 1000);
        try {
            convertObjectToXml (msg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我希望能够将任何消息转换为XML,当我实现时,我收到了这个错误:

  

javax.xml.bind.JAXBException:类sabir.test.jms.entity.Message或其任何超类都知道此上下文。       at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:582)       at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)       在com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)       在com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)       在javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)       at sabir.test.jms.services.ObjectToXml.convertObjectToXml(ObjectToXml.java:31)       在sabir.test.jms.services.ObjectToXml.main(ObjectToXml.java:39)

为什么我会收到此错误?

编辑

我将这行代码编辑成以下内容:

    JAXBContext contextObj = JAXBContext.newInstance(object.getClass());  

现在我没有收到任何错误,但我的程序已经终止。

这是我的Message类:

package sabir.test.jms.entity;
import javax.xml.bind.annotation.XmlAttribute;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;


import java.io.Serializable;


@XmlRootElement  
public class Message implements Serializable{


    private String id; 
    private String type;

    private String senderName;
    private String recieverName;

    private String senderBankName;
    private String recieverBankName;

    private long amount;

    public Message() {} 
    public Message(String id, String type, String senderName, String recieverName, String senderBankName,
            String recieverBankName, long amount) {
        super();
        this.id = id;
        this.type = type;
        this.senderName = senderName;
        this.recieverName = recieverName;
        this.senderBankName = senderBankName;
        this.recieverBankName = recieverBankName;
        this.amount = amount;
    }

    @XmlAttribute  
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @XmlElement
    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    @XmlElement
    public String getRecieverName() {
        return recieverName;
    }


    public void setRecieverName(String recieverName) {
        this.recieverName = recieverName;
    }

    @XmlElement
    public String getSenderBankName() {
        return senderBankName;
    }

    public void setSenderBankName(String senderBankName) {
        this.senderBankName = senderBankName;
    }

    @XmlElement
    public String getRecieverBankName() {
        return recieverBankName;
    }

    public void setRecieverBankName(String recieverBankName) {
        this.recieverBankName = recieverBankName;
    }

    @XmlElement
    public long getAmount() {
        return amount;
    }

    public void setAmount(long amount) {
        this.amount = amount;
    }


}

0 个答案:

没有答案