| @ -0,0 +1,10 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <classpath> | |||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> | |||
| <attributes> | |||
| <attribute name="module" value="true"/> | |||
| </attributes> | |||
| </classpathentry> | |||
| <classpathentry kind="src" path="src"/> | |||
| <classpathentry kind="output" path="bin"/> | |||
| </classpath> | |||
| @ -0,0 +1,17 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <projectDescription> | |||
| <name>MyParser</name> | |||
| <comment></comment> | |||
| <projects> | |||
| </projects> | |||
| <buildSpec> | |||
| <buildCommand> | |||
| <name>org.eclipse.jdt.core.javabuilder</name> | |||
| <arguments> | |||
| </arguments> | |||
| </buildCommand> | |||
| </buildSpec> | |||
| <natures> | |||
| <nature>org.eclipse.jdt.core.javanature</nature> | |||
| </natures> | |||
| </projectDescription> | |||
| @ -0,0 +1,2 @@ | |||
| eclipse.preferences.version=1 | |||
| encoding/<project>=UTF-8 | |||
| @ -0,0 +1,14 @@ | |||
| eclipse.preferences.version=1 | |||
| org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |||
| org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 | |||
| org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |||
| org.eclipse.jdt.core.compiler.compliance=17 | |||
| org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |||
| org.eclipse.jdt.core.compiler.debug.localVariable=generate | |||
| org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |||
| org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |||
| org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | |||
| org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |||
| org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning | |||
| org.eclipse.jdt.core.compiler.release=enabled | |||
| org.eclipse.jdt.core.compiler.source=17 | |||
| @ -0,0 +1,18 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <Empleados> | |||
| <empleado id="1"> | |||
| <apellido>Martinez</apellido> | |||
| <dep>10</dep> | |||
| <salario>1000.33</salario> | |||
| </empleado> | |||
| <empleado id="2"> | |||
| <apellido>Garcia</apellido> | |||
| <dep>11</dep> | |||
| <salario>2000.34</salario> | |||
| </empleado> | |||
| <empleado id="3"> | |||
| <apellido>Navarro</apellido> | |||
| <dep>10</dep> | |||
| <salario>2222.33</salario> | |||
| </empleado> | |||
| </Empleados> | |||
| @ -0,0 +1,66 @@ | |||
| package Escribir; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import javax.xml.transform.*; | |||
| import javax.xml.transform.dom.DOMSource; | |||
| import javax.xml.transform.stream.StreamResult; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.io.OutputStream; | |||
| public class WriteXmlDom { | |||
| public void run() | |||
| throws ParserConfigurationException, TransformerException { | |||
| DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); | |||
| DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); | |||
| // root elements | |||
| Document doc = docBuilder.newDocument(); | |||
| Element rootElement = doc.createElement("empleado"); | |||
| doc.appendChild(rootElement); | |||
| /* | |||
| doc.createAttribute("id"); | |||
| rootElement.appendChild(doc.createAttribute("id")); | |||
| */ | |||
| doc.createElement("apellido"); | |||
| //rootElement.appendChild(doc.createElement("apellido")); | |||
| doc.createElement("dep"); | |||
| rootElement.appendChild(doc.createElement("dep")); | |||
| doc.createElement("salario"); | |||
| rootElement.appendChild(doc.createElement("salario")); | |||
| //...create XML elements, and others... | |||
| // write dom document to a file | |||
| try (FileOutputStream output = | |||
| new FileOutputStream("C:\\Users\\Ivan\\Documents\\Test\\EmpleadosDom.xml")) { | |||
| writeXml(doc, output); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| // write doc to output stream | |||
| private static void writeXml(Document doc, OutputStream output) throws TransformerException { | |||
| TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |||
| Transformer transformer = transformerFactory.newTransformer(); | |||
| DOMSource source = new DOMSource(doc); | |||
| StreamResult result = new StreamResult(output); | |||
| transformer.transform(source, result); | |||
| } | |||
| } | |||
| @ -0,0 +1,32 @@ | |||
| package Leer; | |||
| public class Empleados { | |||
| private int id; | |||
| private String apellido; | |||
| private String dep; | |||
| private float salario; | |||
| public Empleados(int id,String apellido,String dep,float salario) { | |||
| this.id = id; | |||
| this.apellido = apellido; | |||
| this.dep = dep; | |||
| this.salario = salario; | |||
| } | |||
| public String getApellido() { | |||
| return apellido; | |||
| } | |||
| public String getDep() { | |||
| return dep; | |||
| } | |||
| public int getId() { | |||
| return id; | |||
| } | |||
| public float getSalario() { | |||
| return salario; | |||
| } | |||
| } | |||
| @ -0,0 +1,18 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <Empleados> | |||
| <empleado id="1"> | |||
| <apellido>Martinez</apellido> | |||
| <dep>10</dep> | |||
| <salario>1000.33</salario> | |||
| </empleado> | |||
| <empleado id="2"> | |||
| <apellido>Garcia</apellido> | |||
| <dep>11</dep> | |||
| <salario>2000.34</salario> | |||
| </empleado> | |||
| <empleado id="3"> | |||
| <apellido>Navarro</apellido> | |||
| <dep>10</dep> | |||
| <salario>2222.33</salario> | |||
| </empleado> | |||
| </Empleados> | |||
| @ -0,0 +1,68 @@ | |||
| package Leer; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import org.w3c.dom.Node; | |||
| import org.w3c.dom.NodeList; | |||
| public class ParserEmpleados { | |||
| private Empleados[] empleados; | |||
| private File file; | |||
| public boolean parse() { | |||
| /** Parsed controla si se han podido parsear los datos. Inicialmente a false */ | |||
| boolean parsed = false; | |||
| /** Inicializamos a null el array de países */ | |||
| empleados = null; | |||
| try { | |||
| /** Obtenemos una referencia al DocumentBuilderFactory necesaria para parsear mediante DOM */ | |||
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |||
| /** Obtenemos una referencia al DocumentBuilder necesaria para parsear mediante DOM */ | |||
| DocumentBuilder builder = factory.newDocumentBuilder(); | |||
| /** Obtenemos una referencia al Document parseando mediante DOM */ | |||
| file = new File("src/Package/Empleados.xml"); | |||
| System.out.println(file.getAbsolutePath()); | |||
| Document dom = builder.parse(file); | |||
| /** Obtenemos el elemento raíz del documento */ | |||
| Element root = dom.getDocumentElement(); | |||
| NodeList items = root.getElementsByTagName("empleado"); | |||
| empleados = new Empleados[items.getLength()]; | |||
| /** Recorremos cada uno de los nodos */ | |||
| for (int i = 0; i < items.getLength(); i++) { | |||
| /** Obtenemos el nodo de la posición i */ | |||
| Node item = items.item(i); | |||
| Element el = (Element) item; | |||
| int id = Integer.parseInt(el.getAttribute("id")); | |||
| String apellido = el.getElementsByTagName("apellido").item(0).getTextContent(); | |||
| String dep = el.getElementsByTagName("dep").item(0).getTextContent(); | |||
| float salario = Float.parseFloat(el.getElementsByTagName("salario").item(0).getTextContent()); | |||
| /** Con los datos obtenidos, creamos el objeto Country en la posición i del array */ | |||
| empleados[i] = new Empleados(id,apellido,dep,salario); | |||
| } | |||
| /** Si hemos llegado hasta aquí, podemos asegurar que el documento xml ha sido parseado correctamente */ | |||
| parsed = true; | |||
| } catch (ParserConfigurationException pce) { | |||
| System.out.println("PCE"); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| } | |||
| return parsed; | |||
| } | |||
| public Empleados[] getCountries() { | |||
| return this.empleados; | |||
| } | |||
| } | |||
| @ -0,0 +1,30 @@ | |||
| package Main; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import javax.xml.transform.TransformerException; | |||
| import Escribir.WriteXmlDom; | |||
| import Leer.ParserEmpleados; | |||
| public class Main { | |||
| public static void main(String args[]) { | |||
| WriteXmlDom dom = new WriteXmlDom(); | |||
| try { | |||
| dom.run(); | |||
| } catch (ParserConfigurationException e) { | |||
| // TODO Auto-generated catch block | |||
| e.printStackTrace(); | |||
| } catch (TransformerException e) { | |||
| // TODO Auto-generated catch block | |||
| e.printStackTrace(); | |||
| } | |||
| /*ParserEmpleados parser = new ParserEmpleados(); | |||
| if(parser.parse()) { | |||
| System.out.println(parser.getCountries()[2].getApellido()); | |||
| }*/ | |||
| } | |||
| } | |||
| @ -0,0 +1,10 @@ | |||
| /** | |||
| * | |||
| */ | |||
| /** | |||
| * @author Ivan | |||
| * | |||
| */ | |||
| module MyParser { | |||
| requires java.xml; | |||
| } | |||