我的代码如下:
System.out.println("This will save a table to XML data sheet.");
System.out.println("Please pick a table to save: " + listOfTables.toString());
command = scan.nextLine();
if(listOfTables.contains(command))
{
System.out.println("successfuly found table to save: " + command);
try //Java reflection
{
Class<?> myClass = Class.forName(command); // get the class named after their input
Method listMethod = myClass.getDeclaredMethod("list"); // get the list method from the class
Object returnType = listMethod.invoke(myClass, new Object[]{}); // run the list method
ArrayList<Object> objectList = (ArrayList)returnType; // get the arraylist of objects to send to XML
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(myClass);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<?> jaxbElement = new JAXBElement<?>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));
marshaller.marshal(jaxbElement, System.out);
} catch (JAXBException e) {}
}
catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { }
我的问题是:
JAXBElement<?> jaxbElement = new JAXBElement<?>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));
也不:
JAXBElement<myClass> jaxbElement = new JAXBElement<myClass>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));
将编译。那么对于我的 JAXBElement 类型,我需要在 <> 之间放置什么?顺便说一句,我得到:
The constructor JAXBElement<myClass>(QName, Class<myClass>, myClass) refers to the missing type myClass
和:
Cannot instantiate the type JAXBElement<?>