@ -0,0 +1,38 @@ | |||||
target/ | |||||
!.mvn/wrapper/maven-wrapper.jar | |||||
!**/src/main/**/target/ | |||||
!**/src/test/**/target/ | |||||
### IntelliJ IDEA ### | |||||
.idea/modules.xml | |||||
.idea/jarRepositories.xml | |||||
.idea/compiler.xml | |||||
.idea/libraries/ | |||||
*.iws | |||||
*.iml | |||||
*.ipr | |||||
### Eclipse ### | |||||
.apt_generated | |||||
.classpath | |||||
.factorypath | |||||
.project | |||||
.settings | |||||
.springBeans | |||||
.sts4-cache | |||||
### NetBeans ### | |||||
/nbproject/private/ | |||||
/nbbuild/ | |||||
/dist/ | |||||
/nbdist/ | |||||
/.nb-gradle/ | |||||
build/ | |||||
!**/src/main/**/build/ | |||||
!**/src/test/**/build/ | |||||
### VS Code ### | |||||
.vscode/ | |||||
### Mac OS ### | |||||
.DS_Store |
@ -0,0 +1,7 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="Encoding"> | |||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> | |||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> | |||||
</component> | |||||
</project> |
@ -0,0 +1,14 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ExternalStorageConfigurationManager" enabled="true" /> | |||||
<component name="MavenProjectsManager"> | |||||
<option name="originalFiles"> | |||||
<list> | |||||
<option value="$PROJECT_DIR$/pom.xml" /> | |||||
</list> | |||||
</option> | |||||
</component> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/out" /> | |||||
</component> | |||||
</project> |
@ -0,0 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="VcsDirectoryMappings"> | |||||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> | |||||
</component> | |||||
</project> |
@ -0,0 +1,87 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="AutoImportSettings"> | |||||
<option name="autoReloadType" value="SELECTIVE" /> | |||||
</component> | |||||
<component name="ChangeListManager"> | |||||
<list default="true" id="6b7ff7e8-7bd4-4f57-986e-fdd80598347e" name="Changes" comment=""> | |||||
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/README.md" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> | |||||
<change afterPath="$PROJECT_DIR$/src/main/java/org/joanmoncho/Main.java" afterDir="false" /> | |||||
</list> | |||||
<option name="SHOW_DIALOG" value="false" /> | |||||
<option name="HIGHLIGHT_CONFLICTS" value="true" /> | |||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> | |||||
<option name="LAST_RESOLUTION" value="IGNORE" /> | |||||
</component> | |||||
<component name="FileTemplateManagerImpl"> | |||||
<option name="RECENT_TEMPLATES"> | |||||
<list> | |||||
<option value="Class" /> | |||||
</list> | |||||
</option> | |||||
</component> | |||||
<component name="Git.Settings"> | |||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> | |||||
</component> | |||||
<component name="MarkdownSettingsMigration"> | |||||
<option name="stateVersion" value="1" /> | |||||
</component> | |||||
<component name="ProjectId" id="2I57CennSJJCKdn7qm08GHid0v3" /> | |||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true"> | |||||
<ConfirmationsSetting value="2" id="Add" /> | |||||
</component> | |||||
<component name="ProjectViewState"> | |||||
<option name="hideEmptyMiddlePackages" value="true" /> | |||||
<option name="showLibraryContents" value="true" /> | |||||
</component> | |||||
<component name="PropertiesComponent"><![CDATA[{ | |||||
"keyToString": { | |||||
"RunOnceActivity.OpenProjectViewOnStart": "true", | |||||
"RunOnceActivity.ShowReadmeOnStart": "true", | |||||
"SHARE_PROJECT_CONFIGURATION_FILES": "true", | |||||
"WebServerToolWindowFactoryState": "false", | |||||
"nodejs_package_manager_path": "npm" | |||||
} | |||||
}]]></component> | |||||
<component name="RecentsManager"> | |||||
<key name="CreateClassDialog.RecentsKey"> | |||||
<recent name="org.joanmoncho" /> | |||||
</key> | |||||
<key name="MoveFile.RECENT_KEYS"> | |||||
<recent name="C:\Users\JoanMoncho\IdeaProjects\Actividad10" /> | |||||
<recent name="C:\Users\JoanMoncho\IdeaProjects\Actividad10\src\main\java\org\joanmoncho" /> | |||||
</key> | |||||
</component> | |||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> | |||||
<component name="TaskManager"> | |||||
<task active="true" id="Default" summary="Default task"> | |||||
<changelist id="6b7ff7e8-7bd4-4f57-986e-fdd80598347e" name="Changes" comment="" /> | |||||
<created>1669459465687</created> | |||||
<option name="number" value="Default" /> | |||||
<option name="presentableId" value="Default" /> | |||||
<updated>1669459465687</updated> | |||||
<workItem from="1669459467126" duration="1919000" /> | |||||
</task> | |||||
<servers /> | |||||
</component> | |||||
<component name="TypeScriptGeneratedFilesManager"> | |||||
<option name="version" value="3" /> | |||||
</component> | |||||
<component name="Vcs.Log.Tabs.Properties"> | |||||
<option name="TAB_STATES"> | |||||
<map> | |||||
<entry key="MAIN"> | |||||
<value> | |||||
<State /> | |||||
</value> | |||||
</entry> | |||||
</map> | |||||
</option> | |||||
</component> | |||||
</project> |
@ -0,0 +1,38 @@ | |||||
# Aplicación 10 La lectura de este archivo XML | |||||
1. Primero creamos el xml llamado empleados.xml, donde añadimos el id, el nombre, | |||||
el apellido, el departamento y el salario. | |||||
2. Luego creamos una instacia de DocumentBuilderFactory, donde cargara el documento con el método parser(); | |||||
3. Despues obtenemos la lista de nodos con el nombre empleado de todo el documento | |||||
4. Luego con un bucle recorremos la lista | |||||
5. Por ultimo obtenemos las etiquetas y valores utilizando la función getNodeType(); | |||||
## RESULTADO: | |||||
``` | |||||
Numero de empleados: 4 | |||||
ID del empleado: 1 | |||||
Nombre: Joan | |||||
Apellido: Moncho | |||||
Departamento: 10 | |||||
Salario: 999999 | |||||
ID del empleado: 2 | |||||
Nombre: Sergi | |||||
Apellido: Signes | |||||
Departamento: 1 | |||||
Salario: 5000 | |||||
ID del empleado: 3 | |||||
Nombre: Hector | |||||
Apellido: Ferrandiz | |||||
Departamento: 1 | |||||
Salario: 1 | |||||
ID del empleado: 4 | |||||
Nombre: Paco | |||||
Apellido: Mengual | |||||
Departamento: 25 | |||||
Salario: 100 | |||||
``` |
@ -0,0 +1,17 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" | |||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<groupId>org.example</groupId> | |||||
<artifactId>Actividad10</artifactId> | |||||
<version>1.0-SNAPSHOT</version> | |||||
<properties> | |||||
<maven.compiler.source>19</maven.compiler.source> | |||||
<maven.compiler.target>19</maven.compiler.target> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
</properties> | |||||
</project> |
@ -0,0 +1,54 @@ | |||||
package org.joanmoncho; | |||||
import org.w3c.dom.Document; | |||||
import org.w3c.dom.Element; | |||||
import org.w3c.dom.Node; | |||||
import org.w3c.dom.NodeList; | |||||
import javax.xml.parsers.DocumentBuilder; | |||||
import javax.xml.parsers.DocumentBuilderFactory; | |||||
import java.io.*; | |||||
public class Main { | |||||
static File fichero; | |||||
public static void main(String[] args) throws IOException { | |||||
fichero = new File("C:\\Users\\JoanMoncho\\Documents\\ActividadesJuanjo\\empleados.xml"); | |||||
short | |||||
parser(); | |||||
} | |||||
public static void parser(){ | |||||
try { | |||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |||||
Document doc = dBuilder.parse(fichero); | |||||
doc.getDocumentElement().normalize(); | |||||
NodeList nList = doc.getElementsByTagName("empleado"); | |||||
System.out.println("Numero de empleados: " + nList.getLength()); | |||||
for(int temp = 0; temp < nList.getLength(); temp++) { | |||||
Node node = nList.item(temp); | |||||
if(node.getNodeType() == Node.ELEMENT_NODE) { | |||||
Element element = (Element) node; | |||||
System.out.println("\nID del empleado: " + element.getAttribute("id")); | |||||
System.out.println("Nombre: " + element.getElementsByTagName("nombre").item(0).getTextContent()); | |||||
System.out.println("Apellido: " + element.getElementsByTagName("apellido").item(0).getTextContent()); | |||||
System.out.println("Departamento: " + element.getElementsByTagName("departamento").item(0).getTextContent()); | |||||
System.out.println("Salario: " + element.getElementsByTagName("salario").item(0).getTextContent()); | |||||
} | |||||
} | |||||
} catch(Exception e) { | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} |