0

私はでエラーを推測します

T bobj = (T) jaxbUnmarshaller.unmarshal(file);

常にnullを返します

非テンプレートでテストすると、動作してCustomerクラスを返し、テンプレートで使用する場合にのみnullを返します

元のコード

XMLObj<Customer> XMLtool = new XMLObj<Customer>(Customer.class);
Customer c = XMLtool.ConvertXMLToObject("c:\\file2.xml");

public class XMLObj<T> {
    final Class<T> typeParameterClass;
    public XMLObj(Class<T> typeParameterClass) {
        this.typeParameterClass = typeParameterClass;
    }

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

1 に答える 1

0

この行を変更してみてください:

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

これに:

jaxbContext = JAXBContext.newInstance(typeParameterClass);

typeParameterClass.getClass()のクラスのタイプを返しますがjava.lang.Class、それtypeParameterClass自体はCustomerアンマーシャリングするクラスのタイプのクラスを持っています。

于 2012-10-29T10:32:45.987 に答える