java – Using Reflection to create an XML

If I have a class in the following way:

public class ValuacionTool {

private list General data;
private list Ref;
private list MDO;
private list HYP;

public list getDatosgeneral () {
return general data;
public void setDatosgenerales (List datosgeneral) {
this.general data = general data;
public list getRef () {
return ref;
public void setRef (List ref) {
Ref = ref;
public list getMDO () {
Return MDO;
public void setMDO (List mDO) {
MDO = mDO;
public list getHYP () {
Return HYP;
public void setHYP (list hYP) {
HYP = hYP;


And also the respective classes like "DatosGenerales", "Piezas", "Operacion" and "Pintura", which are in the lists, each class has its attributes:

Piece = new pieces ();
piece.setPosition ("15451");
piece.set part number ("SD85AS8Z2A3");
piece.setDescription ("GRILL");
piece.setMonto (1256);
piece.setChangePrice ("0");
ListaPiezas.add (piece);

Painting painting = new painting ();
pintura.setPosicion ("19622");
pintura.setDescripcion ("REC.ESPEJO LATER.D. PINT.SUST PRINTED");
painting.setMonto (3657.1);
painting.setUt (6);
ListPainting.add (painting);

I have to develop an XML that the class does not know this only needs the attributes and values ​​to create it.

Try this, but it does not work for me:

try // Java Reflection
class myClass = Class.forName (command); // Name the class as you typed it
Method listMethod = myClass.getDeclaredMethod ("list"); // Get the list method from the class
Object returnType = listMethod.invoke (my class, new object[]{}); // execute the list method
ArrayList objectList = (ArrayList) returnType; // Get the array of objects to send to XML
try it
JAXBContext jaxbContext = JAXBContext.newInstance (my class);
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) {}

I hope you can help me, thank you!