0

http://www.mkyong.com/java/jaxb-hello-world-example/ ファイルまたは (Result)file にエラー java.io.File cannot be cast to javax.xml.transform.Result が表示されます。何が正しいですか書き込み?

File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

//output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(boject, file);

スタックトレース

java.lang.ClassCastException: java.io.File cannot be cast to javax.xml.transform.Result
    at martin.XMLObj.ConvertObjectToXML(XMLObj.java:26)
    at martin.Helloworld.main(Helloworld.java:100)

元のコード

Customer customer = new Customer();
customer.setId(100);
customer.setName("mkyong");
customer.setAge(29);
XMLObj<Customer> XMLtool = new XMLObj<Customer>(Customer.class);
try {
    XMLtool.ConvertObjectToXML("c:\\file5.xml", customer);
}
catch(Exception ex)
{
    ex.printStackTrace();
}

.

public class XMLObj<T> {
    final Class<T> typeParameterClass;
    public XMLObj(Class<T> typeParameterClass) {
        this.typeParameterClass = typeParameterClass;
    }
    public void ConvertObjectToXML(String path, T boject)
    {
        try {
            File file = new File(path);
            JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass.getClass());
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            //output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            FileOutputStream fs = new FileOutputStream(file);
            jaxbMarshaller.marshal(boject, fs);
            fs.flush();
            fs.close();

            //jaxbMarshaller.marshal(boject, System.out);
        } catch (JAXBException e) {
            //e.printStackTrace();
            Logger.getInstance().process_message(e.getMessage());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Logger.getInstance().process_message(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public T ConvertXMLToObject(String path)
    {
        //Convert XML to Object
        try {
            File file = new File(path);
            if(file.exists())
            {
                JAXBContext jaxbContext;
                jaxbContext = JAXBContext.newInstance(typeParameterClass.getClass());
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                T bobj = (T) jaxbUnmarshaller.unmarshal(file);
                System.out.println(bobj);
                return bobj;
            }
            else
                Logger.getInstance().process_message("File not exist in ConvertObjectToXML");
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            Logger.getInstance().process_message(e.getMessage());
        }
        return null;
    }
}
4

2 に答える 2

0

行が原因で空のファイルを取得しています

JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass.getClass());

これをに変更

JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);

問題は、オブジェクトを非整列化できないtypeParameterClass.getClass()クラスのタイプがあることです。java.lang.ClassCustomer

于 2012-10-29T10:48:16.930 に答える
0

最初に API を読んだことがありますか?

の 2 番目のパラメーターはMarshaller#marshal()、ファイルを取得していません。

marshal()あなたが利用できる他のオーバーロードがたくさんありますmarshall(Object, OutputStream)。実際にどのように書くかは、自分で理解できるはずです。

于 2012-10-29T03:22:47.787 に答える