Está en la página 1de 4

Cdigo java para el uso de XML Espero continuar con el tema y hacer un ejemplo con JAXB.

En otro post tratarde hablar ms de esa tecnologa con ayuda de NetBeans y/o Eclipse. Crear una clase java bsica. Datos1.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package modelo.pruebas; //import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * * @author yo */ @XmlRootElement public class Datos1 { private double valor,tasa; private int periodo; /** * @return the valor */ public double getValor() { return valor; } /** * @param valor the valor to set */ @XmlElement public void setValor(double valor) { this.valor = valor; } /** * @return the tasa */ public double getTasa() { return tasa; } /** * @param tasa the tasa to set */ @XmlElement public void setTasa(double tasa) { this.tasa = tasa; } /** * @return the periodo */ public int getPeriodo() { return periodo; } /** * @param periodo the periodo to set */ @XmlElement public void setPeriodo(int periodo) {

this.periodo = periodo; } } Convertir esa clase a xml. ControlDatos1Xml.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package control.pruebas; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import modelo.pruebas.Datos1; import javax.swing.JOptionPane; /** * * @author yo */ public class ControlDatos1Xml { Datos1 datos= new Datos1(); public static void main(String[] args){ ControlDatos1Xml control= new ControlDatos1Xml(); double v=0,t=0; int p; v=Double.parseDouble(JOptionPane.showInputDialog("valor")); t=Double.parseDouble(JOptionPane.showInputDialog("tasa")); p=Integer.parseInt(JOptionPane.showInputDialog("periodo")); control.datos.setPeriodo(p); control.datos.setTasa(t); control.datos.setValor(v); //-----------------------------------try { File file = new File("/home/user/PruebasClases/dat1.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Datos1.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(control.datos, file); jaxbMarshaller.marshal(control.datos, System.out); } catch (JAXBException e) { e.printStackTrace(); } JOptionPane.showMessageDialog(null, "XML generado"); //-----------------------------------

} Leer el XML como si fuera objeto. ControlDatos1Class.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package control.pruebas; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import modelo.pruebas.Datos1; /** * * @author yo */ public class ControlDatos1Class { public static void main(String[] args){ try { File file = new File("/home/user/PruebasClases/dat1.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Datos1.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Datos1 datos = (Datos1) jaxbUnmarshaller.unmarshal(file); System.out.println(datos); //--------------------------------------if(datos.getValor()<=0){ System.out.println("El valor no debe ser menor o igual a cero"); } //-----------------------------------else{ System.out.println("valor -->"+datos.getValor()); System.out.println("tasa -->"+datos.getTasa()); System.out.println("periodo -->"+datos.getPeriodo()); } } catch (JAXBException e) { e.printStackTrace(); } //----------------------------------} }

También podría gustarte