java - Jaxb Marshalling for newbies -


all i'm trying achieve marshal 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 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 class try instantiate of other classes , marshall data 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 =  "johndoe@gmail.com";         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 never generates .xml file. need solve small issue, know face problem not in best way, i'm stucked here.

thanks in advance!

update #2

double check java.io.file being created think is. can check follows:

file file = new file("archivo.xml"); system.out.println(file.getabsolutepath()); 

update #1

you may find marshalling directly java.io.file directly:


you need close fileoutputstream after marshal operation.

    fileoutputstream out = new fileoutputstream("archivo.xml");     marshaller.marshal(config, out);     out.close(); 

btw don't need @xmlelement annotations default public properties , fields treated mapped default.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -