Hola a todos, hoy os voy a explicar como podemos crear un XML con JDOM2.
Con JDOM2 es fácil crear un XML, lo primero que necesitamos es descargar el JAR de JDOM desde su página oficial.
Debes añadir ese JAR como librería, te dejo un manual por sino sabes como se hace en netbeans.
Lo primero que debemos hacer es crear el elemento base:
// Creamos el elemento base Element concesionario = new Element("concesionario"); Document doc = new Document(concesionario);
Después, voy a crear un elemento hijo:
// Elemento hijo Element coches = new Element("coches"); concesionario.addContent(coches);
Lo añadimos al elemento raíz (concesionario).
Ahora vamos a crear un coche:
// Creamos el coche 1 Element coche = new Element("coche"); coche.setAttribute("matricula", "1111AAA"); Element marca = new Element("marca"); marca.setText("AUDI"); Element precio = new Element("precio"); precio.setText("30000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche);
Con setAttribute, creamos un atributo en un elemento del XML, recuerda que se debe añadir al elemento padre (en nuestro caso, coches).
Los nodos hijos (marca y precio) deben agregarse al elemento padre (coche).
Creamos el resto de coches:
// Creamos el coche 2 coche = new Element("coche"); coche.setAttribute("matricula", "2222BBB"); marca = new Element("marca"); marca.setText("SEAT"); precio = new Element("precio"); precio.setText("10000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el coche 3 coche = new Element("coche"); coche.setAttribute("matricula", "3333CCC"); marca = new Element("marca"); marca.setText("BMW"); precio = new Element("precio"); precio.setText("20000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el coche 4 coche = new Element("coche"); coche.setAttribute("matricula", "4444DDD"); marca = new Element("marca"); marca.setText("TOYOTA"); precio = new Element("precio"); precio.setText("10000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche);
Ahora tenemos que crear el fichero XML con la clase XMLOutputter de JDOM2.
Recuerda que necesitaras el elemento raíz que hemos creado al inicio (objeto Document).
// Creamos el fichero XML XMLOutputter xml = new XMLOutputter(); xml.setFormat(Format.getPrettyFormat()); xml.output(doc, new FileWriter("concesionario.xml"));
También os dejo un vídeo para que sea mas sencillo de seguir:
https://www.youtube.com/watch?v=u4ALN2bRHG4
Os dejo el ejemplo completo:
import java.io.FileWriter; import java.io.IOException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class CrearXMLJDOM2 { public static void main(String[] args) { try { // Creamos el elemento base Element concesionario = new Element("concesionario"); Document doc = new Document(concesionario); // Elemento hijo Element coches = new Element("coches"); concesionario.addContent(coches); // Creamos el coche 1 Element coche = new Element("coche"); coche.setAttribute("matricula", "1111AAA"); Element marca = new Element("marca"); marca.setText("AUDI"); Element precio = new Element("precio"); precio.setText("30000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el coche 2 coche = new Element("coche"); coche.setAttribute("matricula", "2222BBB"); marca = new Element("marca"); marca.setText("SEAT"); precio = new Element("precio"); precio.setText("10000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el coche 3 coche = new Element("coche"); coche.setAttribute("matricula", "3333CCC"); marca = new Element("marca"); marca.setText("BMW"); precio = new Element("precio"); precio.setText("20000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el coche 4 coche = new Element("coche"); coche.setAttribute("matricula", "4444DDD"); marca = new Element("marca"); marca.setText("TOYOTA"); precio = new Element("precio"); precio.setText("10000"); coche.addContent(marca); coche.addContent(precio); coches.addContent(coche); // Creamos el fichero XML XMLOutputter xml = new XMLOutputter(); xml.setFormat(Format.getPrettyFormat()); xml.output(doc, new FileWriter("concesionario.xml")); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }
Espero que os sea de ayuda. Si tenéis dudas, preguntad. Estamos para ayudarte.
Deja una respuesta