Van icon

JAVA JAXB

Links


    //Uso de JAXB En Netbeans
    Click derecho proyecto > New > Jaxb Binding
    Set name and schema file or URL, finish. Usually a XSD file
    This generates a package with the classes


    //JAVA <
    public class Tarea {

 
        public  Tarea() throws MalformedURLException, IOException {
          Grupo documento;
            try {
                JAXBContext context = JAXBContext.newInstance(Grupo.class);
                Unmarshaller convertidorDeXMLToObjecto = context.createUnmarshaller();
     
                  // partir de un documento XML obtenemos el objeto.
                URL url = new URL("http://www.ferminvelez.es/E2/grupo.xml"); // ojo poner el protocolo (http)           
                URLConnection conexion= url.openConnection();     
         
             documento = (Grupo) convertidorDeXMLToObjecto.unmarshal( conexion.getInputStream()); // solo le decimos donde sacar los datos = DE LA URL              
            System.out.println("Alumnos del grupo:");
    
             List<Alumno> alumnos= documento.getListaAlumnos(); //obtener el listado de alumnos del grupo
             for ( int i=0; i <alumnos.size(); i++) verUnAlumno(alumnos.get(i));//imprimir cada alumno   
             
            }
            
            catch (JAXBException ex) {  System.out.println("JAXBE : "+ex.getMessage());  }
        }
        
      
         static void verUnAlumno(Alumno a)
        {
            System.out.print ("Nombre: "+a.getNombre()+" ");
            System.out.print ("\tApellido: "+a.getNombrePadre()+"\tID: "+a.getId()+"\tNota Media: "+a.getNotaMedia()+"\n");   
         
        }

    }


    //GENERATED GRUPO
    import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for grupo complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="grupo">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="listaAlumnos" type="{}alumno" maxOccurs="unbounded" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "grupo", propOrder = {
    "listaAlumnos"
})

//FALTA EL @Xml root element???
public class Grupo {

    protected List<Alumno> listaAlumnos;

    public List<Alumno> getListaAlumnos() {
        if (listaAlumnos == null) {
            listaAlumnos = new ArrayList<Alumno>();
        }
        return this.listaAlumnos;
    }

}



    //ALUMNO GENERATED

    package generated;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlType;

    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "alumno", propOrder = {
        "nombre",
        "nombrePadre",
        "id",
        "notaMedia"
    })
    public class Alumno {

        protected String nombre;
        protected String nombrePadre;
        protected int id;
        protected double notaMedia;

        /**
        * Gets the value of the nombre property.
        * 
        * @return
        *     possible object is
        *     {@link String }
        *     
        */
        public String getNombre() {
            return nombre;
        }

        /**
        * Sets the value of the nombre property.
        * 
        * @param value
        *     allowed object is
        *     {@link String }
        *     
        */
        public void setNombre(String value) {
            this.nombre = value;
        }

        /**
        * Gets the value of the nombrePadre property.
        * 
        * @return
        *     possible object is
        *     {@link String }
        *     
        */
        public String getNombrePadre() {
            return nombrePadre;
        }

        /**
        * Sets the value of the nombrePadre property.
        * 
        * @param value
        *     allowed object is
        *     {@link String }
        *     
        */
        public void setNombrePadre(String value) {
            this.nombrePadre = value;
        }

        /**
        * Gets the value of the id property.
        * 
        */
        public int getId() {
            return id;
        }

        /**
        * Sets the value of the id property.
        * 
        */
        public void setId(int value) {
            this.id = value;
        }

        /**
        * Gets the value of the notaMedia property.
        * 
        */
        public double getNotaMedia() {
            return notaMedia;
        }

        /**
        * Sets the value of the notaMedia property.
        * 
        */
        public void setNotaMedia(double value) {
            this.notaMedia = value;
        }

    }