Switch from XML or XSD to Java class? [closed]

-1

Good! I need to pass an 'XSD' document, or XML to java classes and vice versa, it has to be using SAX, does anyone know how?

    
asked by Jpachecod 04.10.2017 в 18:39
source

1 answer

2

Use the library XStream , it serves to parse from XML to java objects and vice versa. I recommend you read the Alstream tutorial of Xstream since with that you learn almost everything about how to use it. This is not SAX but it is more powerful.

Mini example of a library:

package ejemplo.xstream;
public class Libro {
    private String tipo;
    private String titulo;
    private String autor;
    public Libro(String tipo, String titulo, String autor) {
        this.tipo = tipo;
        this.titulo = titulo;
        this.autor = autor;
    }
    public String getTipo() {
        return tipo;
    }
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getAutor() {
        return autor;
    }
    public void setAutor(String autor) {
        this.autor = autor;
    }

}
package ejemplo.xstream;
import java.util.ArrayList;
import java.util.List;
public class Biblioteca {
    List<Libro> libros=new ArrayList<Libro>();
    public Biblioteca(Libro libro) {
        this.libros.add(libro);
    }
    public List<Libro> getLibros() {
        return libros;
    }
    public void add(Libro libro) {
        this.libros.add(libro);
    }
}
package ejemplo.xstream;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class StringConverter implements SingleValueConverter {
    @Override
    public boolean canConvert(Class arg0) {
        return arg0.equals(String.class);
    }
    @Override
    public Object fromString(String arg0) {
        return arg0;
    }
    @Override
    public String toString(Object arg0) {
        return (String) arg0;
    }

}
package ejemplo.xstream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XstreamTransformacion {
    private static XStream configuracion(){
        XStream xstream=new XStream(new DomDriver());
        xstream.alias("biblioteca", Biblioteca.class);
        xstream.alias("libro", Libro.class);
        xstream.addImplicitCollection(Biblioteca.class, "libros");
        xstream.useAttributeFor(Libro.class, "tipo");
        xstream.registerConverter(new StringConverter());   
        return xstream;
    }
    public static String toXML(Biblioteca biblioteca){
        XStream xstream=XstreamTransformacion.configuracion();
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+xstream.toXML(biblioteca);
    }             
    public static Biblioteca fromXML(String xml){
        XStream xstream=XstreamTransformacion.configuracion();
        return (Biblioteca)xstream.fromXML(xml);
    }
}
package ejemplo.xstream;
public class Pruebas {
    public static void main(String[] args) {
        Biblioteca biblioteca=new Biblioteca(new Libro("Novela", "Don quijote", "Miguel de cervantes"));
        biblioteca.add(new Libro("teatro", "Romeo y julieta", "William Shakespeare"));
        biblioteca.add(new Libro("cuento", "El Aleph", "Jorge Luis Borges"));
        biblioteca.add(new Libro("poemas", "Antologia", "Mario Benedetti"));
        String xml=XstreamTransformacion.toXML(biblioteca);
        System.out.println(xml);
    }

}
    
answered by 04.10.2017 в 18:48