|
@ -0,0 +1,133 @@ |
|
|
|
|
|
package ReaderXMLFicheros; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.text.Document; |
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
|
|
import javax.xml.transform.Transformer; |
|
|
|
|
|
import javax.xml.transform.TransformerFactory; |
|
|
|
|
|
import javax.xml.transform.dom.DOMSource; |
|
|
|
|
|
import javax.xml.transform.stream.StreamResult; |
|
|
|
|
|
|
|
|
|
|
|
import org.w3c.dom.Attr; |
|
|
|
|
|
import org.w3c.dom.Element; |
|
|
|
|
|
import org.w3c.dom.Node; |
|
|
|
|
|
import org.w3c.dom.NodeList; |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Ejercicio 10. La lectura de este archivo XML |
|
|
|
|
|
* Ejercicio 11. A partir del fichero de objetos Persona crea un XML usando DOM |
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
|
|
|
|
public class ReaderXMLFicheros { |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException{ |
|
|
|
|
|
|
|
|
|
|
|
String filePath = "C:\\Users\\jandr\\Desktop\\Acceso a datos\\Container Ficheros\\Empleados.xml"; |
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
|
|
|
|
|
|
// 10. Lectura archivo .xml |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
|
|
|
|
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
|
|
|
|
|
Document doc = (Document) dBuilder.parse(file); |
|
|
|
|
|
|
|
|
|
|
|
// Accede al nodo raíz del documento y Elimina nodos vacíos y combina adyacentes en caso de que los hubiera |
|
|
|
|
|
((org.w3c.dom.Document) doc).getDocumentElement().normalize(); |
|
|
|
|
|
|
|
|
|
|
|
// Almacenar el elemento que le indicaremos como parámetro |
|
|
|
|
|
NodeList nList = ((org.w3c.dom.Document) doc).getElementsByTagName("empleado"); |
|
|
|
|
|
|
|
|
|
|
|
// Una vez tenemos almacenados los datos del nodo “empleado” podemos leer su contenido teniendo en cuenta que |
|
|
|
|
|
//este código depende de que conozcamos la estructura y etiquetas utilizadas. |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nList.getLength(); i++) { |
|
|
|
|
|
|
|
|
|
|
|
Node node = nList.item(i); |
|
|
|
|
|
|
|
|
|
|
|
if (node.getNodeType() == Node.ELEMENT_NODE) { |
|
|
|
|
|
Element eElement = (Element) node; |
|
|
|
|
|
|
|
|
|
|
|
if(eElement.hasChildNodes()) { |
|
|
|
|
|
NodeList nl = node.getChildNodes(); |
|
|
|
|
|
|
|
|
|
|
|
for(int j=0; j<nl.getLength(); j++) { |
|
|
|
|
|
Node nd = nl.item(j); |
|
|
|
|
|
System.out.println(nd.getTextContent()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 11. A partir del fichero de objetos Persona crea un XML usando DOM |
|
|
|
|
|
/* Si quisiéramos escribir un archivo XML siguiendo la misma estructura del concesionario deberíamos |
|
|
|
|
|
* instanciar las clases DocumentBuilderFactory, DocumentBuilder y Document, definir toda la estructura |
|
|
|
|
|
* del archivo (siempre dentro de un bloque try/catch) y por último instanciar las clases TransformerFactory, |
|
|
|
|
|
* Transformer, DOMSource y StreamResult para crear el archivo.*/ |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
|
|
|
|
DocumentBuilder db = dbf.newDocumentBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
Document docCreate = (Document) db.newDocument(); |
|
|
|
|
|
|
|
|
|
|
|
// Definimos el elemento raíz del documento |
|
|
|
|
|
Element eRaiz = ((org.w3c.dom.Document) docCreate).createElement("empresa"); |
|
|
|
|
|
((Node) docCreate).appendChild(eRaiz); |
|
|
|
|
|
|
|
|
|
|
|
// Definimos el nodo que contendrá los elementos |
|
|
|
|
|
Element empleado = (Element) ((org.w3c.dom.Document) docCreate).createElement("empleado"); |
|
|
|
|
|
eRaiz.appendChild(empleado); |
|
|
|
|
|
|
|
|
|
|
|
// Atributo para el nodo 'Empleado' |
|
|
|
|
|
Attr attr = ((org.w3c.dom.Document) docCreate).createAttribute("id"); |
|
|
|
|
|
attr.setValue("1"); |
|
|
|
|
|
empleado.setAttributeNode(attr); |
|
|
|
|
|
|
|
|
|
|
|
// Definimos cada uno de los elementos y le asignamos un valor |
|
|
|
|
|
Element apellido = ((org.w3c.dom.Document) docCreate).createElement("apellido"); |
|
|
|
|
|
apellido.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Gonzalez")); |
|
|
|
|
|
apellido.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Lopez")); |
|
|
|
|
|
apellido.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Garcia")); |
|
|
|
|
|
empleado.appendChild(apellido); |
|
|
|
|
|
|
|
|
|
|
|
Element departamento = ((org.w3c.dom.Document) docCreate).createElement("departamento"); |
|
|
|
|
|
departamento.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Informatico")); |
|
|
|
|
|
departamento.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Consejeria")); |
|
|
|
|
|
departamento.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("Oficinas")); |
|
|
|
|
|
empleado.appendChild(departamento); |
|
|
|
|
|
|
|
|
|
|
|
Element salario = ((org.w3c.dom.Document) docCreate).createElement("salario"); |
|
|
|
|
|
salario.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("900.00")); |
|
|
|
|
|
salario.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("1200.00")); |
|
|
|
|
|
salario.appendChild(((org.w3c.dom.Document) docCreate).createTextNode("3000.00")); |
|
|
|
|
|
empleado.appendChild(salario); |
|
|
|
|
|
|
|
|
|
|
|
// Clases necesarias finalizar la creación del archivo XML |
|
|
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
|
|
|
DOMSource source = new DOMSource((Node) docCreate); |
|
|
|
|
|
StreamResult result = new StreamResult(new File("empleados_nuevos.xml")); |
|
|
|
|
|
transformer.transform(source, result); |
|
|
|
|
|
|
|
|
|
|
|
} catch(Exception e) { |
|
|
|
|
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch(Exception e) { |
|
|
|
|
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |