Reputation: 296
All I'm trying to achieve is simply to marshal to a xml file
Contacto.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Contacto {
@XmlElement
public String nombre;
@XmlElement
public String telefono;
@XmlElement
public String email;
@XmlElement
public String direccion;
}
Actividad.java
import java.util.Calendar;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Actividad {
@XmlElement
public Calendar fecha;
@XmlElement
public String lugar;
@XmlElement
public String motivo;
@XmlElement
public Contacto participante;
}
Agenda.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Agenda {
@XmlElement
public String nombrePropietario;
@XmlElement
public Actividad actividad;
@XmlElement
public Contacto contacto;
}
This is the XML Schema generated using JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="actividad" type="actividad"/>
<xs:element name="agenda" type="agenda"/>
<xs:element name="contacto" type="contacto"/>
<xs:complexType name="actividad">
<xs:sequence>
<xs:element name="fecha" type="xs:dateTime" minOccurs="0"/>
<xs:element name="lugar" type="xs:string" minOccurs="0"/>
<xs:element name="motivo" type="xs:string" minOccurs="0"/>
<xs:element name="participante" type="contacto" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="contacto">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="0"/>
<xs:element name="telefono" type="xs:string" minOccurs="0"/>
<xs:element name="email" type="xs:string" minOccurs="0"/>
<xs:element name="direccion" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="agenda">
<xs:sequence>
<xs:element name="nombrePropietario" type="xs:string" minOccurs="0"/>
<xs:element ref="actividad" minOccurs="0"/>
<xs:element ref="contacto" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="principal">
<xs:sequence/>
</xs:complexType>
</xs:schema>
And this is the Class where I try to instantiate some of the other classes and marshall their data to a xml file:
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.util.Calendar;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.lang.Object;
import java.io.IOException;
public class Principal {
public static void main(String[] args) {
Contacto contacto = new Contacto();
contacto.nombre = "John Doe";
contacto.telefono = "911";
contacto.email = "[email protected]";
contacto.direccion = "742 Evergreen Terrace";
Actividad actividad = new Actividad();
Calendar cal = Calendar.getInstance();
cal.set(2013,04,17,15,30,00);
actividad.fecha = cal;
actividad.lugar = "General Pico";
actividad.motivo = "Reunion";
actividad.participante = contacto;
Agenda agenda = new Agenda();
agenda.nombrePropietario = "John Smith";
agenda.actividad = actividad;
agenda.contacto = contacto;
try {
File file = new File("archivo.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Contacto.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// Salida
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
OutputStream os = new FileOutputStream(file);
jaxbMarshaller.marshal(contacto, os);
jaxbMarshaller.marshal(contacto, System.out);
/*
jaxbMarshaller.marshal(actividad, os);
jaxbMarshaller.marshal(actividad, System.out);
jaxbMarshaller.marshal(agenda, os);
jaxbMarshaller.marshal(agenda, System.out);
*/
} catch (Exception e) { //JAXBException e
e.printStackTrace();
}
}
}
The problem is it never generates the .xml file. I just need to solve this small issue, I know I probably face the problem not in the best way, but i'm stucked here.
Thanks in advance!
Upvotes: 1
Views: 368
Reputation: 148977
UPDATE #2
Double check the java.io.File
is being created where you think it is. You can check this as follows:
File file = new File("archivo.xml");
System.out.println(file.getAbsolutePath());
UPDATE #1
You may find marshalling directly to a java.io.File
directly:
You need to close the FileOutputStream
after the marshal operation.
FileOutputStream out = new FileOutputStream("archivo.xml");
marshaller.marshal(config, out);
out.close();
BTW you don't need all those @XmlElement
annotations as by default all public properties and fields will be treated as mapped by default.
Upvotes: 2