--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<?xml-stylesheet href="catalogo.xsl" type="text/xsl"?>
+<catalogo>
+ <cd valoracion="10">
+ <titulo>Empire Burlesque</titulo>
+ <artista>Bob Dylan</artista>
+ <pais>USA</pais>
+ <discografica>Columbia</discografica>
+ <precio>10.90</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Bulería</titulo>
+ <artista>David Bisbal</artista>
+ <pais>España</pais>
+ <discografica>Vale</discografica>
+ <precio>20</precio>
+ <anio>2004</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Hide your heart</titulo>
+ <artista>Bonnie Tyler</artista>
+ <paisy>UK</paisy>
+ <discografica>CBS Records</discografica>
+ <precio>9.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Greatest Hits</titulo>
+ <artista>Dolly Parton</artista>
+ <paisy>USA</paisy>
+ <discografica>RCA</discografica>
+ <precio>9.90</precio>
+ <anio>1982</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>Still got the blues</titulo>
+ <artista>Gary Moore</artista>
+ <paisy>UK</paisy>
+ <discografica>Virgin records</discografica>
+ <precio>10.20</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Eros</titulo>
+ <artista>Eros Ramazzotti</artista>
+ <paisy>EU</paisy>
+ <discografica>BMG</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>One night only</titulo>
+ <artista>Bee Gees</artista>
+ <paisy>UK</paisy>
+ <discografica>Polydor</discografica>
+ <precio>10.90</precio>
+ <anio>1998</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Sylvias Mother</titulo>
+ <artista>Dr.Hook</artista>
+ <paisy>UK</paisy>
+ <discografica>CBS</discografica>
+ <precio>8.10</precio>
+ <anio>1973</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Maggie May</titulo>
+ <artista>Rod Stewart</artista>
+ <paisy>UK</paisy>
+ <discografica>Pickwick</discografica>
+ <precio>8.50</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Romanza</titulo>
+ <artista>Andrea Bocelli</artista>
+ <paisy>EU</paisy>
+ <discografica>Polydor</discografica>
+ <precio>10.80</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>When a man loves a woman</titulo>
+ <artista>Percy Sledge</artista>
+ <paisy>USA</paisy>
+ <discografica>Atlantic</discografica>
+ <precio>8.70</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Black angel</titulo>
+ <artista>Savage Rose</artista>
+ <paisy>EU</paisy>
+ <discografica>Mega</discografica>
+ <precio>10.90</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="4">
+ <titulo>1999 Grammy Nominees</titulo>
+ <artista>Many</artista>
+ <paisy>USA</paisy>
+ <discografica>Grammy</discografica>
+ <precio>10.20</precio>
+ <anio>1999</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>For the good times</titulo>
+ <artista>Kenny Rogers</artista>
+ <paisy>UK</paisy>
+ <discografica>Mucik Master</discografica>
+ <precio>8.70</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Big Willie style</titulo>
+ <artista>Will Smith</artista>
+ <paisy>USA</paisy>
+ <discografica>Columbia</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Tupelo Honey</titulo>
+ <artista>Van Morrison</artista>
+ <paisy>UK</paisy>
+ <discografica>Polydor</discografica>
+ <precio>8.20</precio>
+ <anio>1971</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Soulsville</titulo>
+ <artista>Jorn Hoel</artista>
+ <paisy>Norway</paisy>
+ <discografica>WEA</discografica>
+ <precio>7.90</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>The very best of</titulo>
+ <artista>Cat Stevens</artista>
+ <paisy>UK</paisy>
+ <discografica>Island</discografica>
+ <precio>8.90</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Stop</titulo>
+ <artista>Sam Brown</artista>
+ <paisy>UK</paisy>
+ <discografica>A and M</discografica>
+ <precio>8.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Bridge of Spies</titulo>
+ <artista>T`Pau</artista>
+ <paisy>UK</paisy>
+ <discografica>Siren</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Private Dancer</titulo>
+ <artista>Tina Turner</artista>
+ <paisy>UK</paisy>
+ <discografica>Capitol</discografica>
+ <precio>8.90</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Midt om natten</titulo>
+ <artista>Kim Larsen</artista>
+ <paisy>EU</paisy>
+ <discografica>Medley</discografica>
+ <precio>7.80</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Pavarotti Gala Concert</titulo>
+ <artista>Luciano Pavarotti</artista>
+ <paisy>UK</paisy>
+ <discografica>DECCA</discografica>
+ <precio>9.90</precio>
+ <anio>1991</anio>
+ </cd>
+ <cd valoracion="1">
+ <titulo>The dock of the bay</titulo>
+ <artista>Otis Redding</artista>
+ <paisy>USA</paisy>
+ <discografica>Atlantic</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Picture book</titulo>
+ <artista>Simply Red</artista>
+ <paisy>EU</paisy>
+ <discografica>Elektra</discografica>
+ <precio>7.20</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Red</titulo>
+ <artista>The Communards</artista>
+ <paisy>UK</paisy>
+ <discografica>London</discografica>
+ <precio>7.80</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="10">
+ <titulo>Unchain my heart</titulo>
+ <artista>Joe Cocker</artista>
+ <paisy>USA</paisy>
+ <discografica>EMI</discografica>
+ <precio>8.20</precio>
+ <anio>1987</anio>
+ </cd>
+</catalogo>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>cursoxml</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>
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<?xml-stylesheet href="catalogo.xsl" type="text/xsl"?>
+<catalogo>
+ <cd valoracion="10">
+ <titulo>Empire Burlesque</titulo>
+ <artista>Bob Dylan</artista>
+ <pais>USA</pais>
+ <discografica>Columbia</discografica>
+ <precio>10.90</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Bulería</titulo>
+ <artista>David Bisbal</artista>
+ <pais>España</pais>
+ <discografica>Vale</discografica>
+ <precio>20</precio>
+ <anio>2004</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Hide your heart</titulo>
+ <artista>Bonnie Tyler</artista>
+ <pais>UK</pais>
+ <discografica>CBS Records</discografica>
+ <precio>9.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Greatest Hits</titulo>
+ <artista>Dolly Parton</artista>
+ <pais>USA</pais>
+ <discografica>RCA</discografica>
+ <precio>9.90</precio>
+ <anio>1982</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>Still got the blues</titulo>
+ <artista>Gary Moore</artista>
+ <pais>UK</pais>
+ <discografica>Virgin records</discografica>
+ <precio>10.20</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Eros</titulo>
+ <artista>Eros Ramazzotti</artista>
+ <pais>EU</pais>
+ <discografica>BMG</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>One night only</titulo>
+ <artista>Bee Gees</artista>
+ <pais>UK</pais>
+ <discografica>Polydor</discografica>
+ <precio>10.90</precio>
+ <anio>1998</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Sylvias Mother</titulo>
+ <artista>Dr.Hook</artista>
+ <pais>UK</pais>
+ <discografica>CBS</discografica>
+ <precio>8.10</precio>
+ <anio>1973</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Maggie May</titulo>
+ <artista>Rod Stewart</artista>
+ <pais>UK</pais>
+ <discografica>Pickwick</discografica>
+ <precio>8.50</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Romanza</titulo>
+ <artista>Andrea Bocelli</artista>
+ <pais>EU</pais>
+ <discografica>Polydor</discografica>
+ <precio>10.80</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>When a man loves a woman</titulo>
+ <artista>Percy Sledge</artista>
+ <pais>USA</pais>
+ <discografica>Atlantic</discografica>
+ <precio>8.70</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Black angel</titulo>
+ <artista>Savage Rose</artista>
+ <pais>EU</pais>
+ <discografica>Mega</discografica>
+ <precio>10.90</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="4">
+ <titulo>1999 Grammy Nominees</titulo>
+ <artista>Many</artista>
+ <pais>USA</pais>
+ <discografica>Grammy</discografica>
+ <precio>10.20</precio>
+ <anio>1999</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>For the good times</titulo>
+ <artista>Kenny Rogers</artista>
+ <pais>UK</pais>
+ <discografica>Mucik Master</discografica>
+ <precio>8.70</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Big Willie style</titulo>
+ <artista>Will Smith</artista>
+ <pais>USA</pais>
+ <discografica>Columbia</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Tupelo Honey</titulo>
+ <artista>Van Morrison</artista>
+ <pais>UK</pais>
+ <discografica>Polydor</discografica>
+ <precio>8.20</precio>
+ <anio>1971</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Soulsville</titulo>
+ <artista>Jorn Hoel</artista>
+ <pais>Norway</pais>
+ <discografica>WEA</discografica>
+ <precio>7.90</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>The very best of</titulo>
+ <artista>Cat Stevens</artista>
+ <pais>UK</pais>
+ <discografica>Island</discografica>
+ <precio>8.90</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Stop</titulo>
+ <artista>Sam Brown</artista>
+ <pais>UK</pais>
+ <discografica>A and M</discografica>
+ <precio>8.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Bridge of Spies</titulo>
+ <artista>T`Pau</artista>
+ <pais>UK</pais>
+ <discografica>Siren</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Private Dancer</titulo>
+ <artista>Tina Turner</artista>
+ <pais>UK</pais>
+ <discografica>Capitol</discografica>
+ <precio>8.90</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Midt om natten</titulo>
+ <artista>Kim Larsen</artista>
+ <pais>EU</pais>
+ <discografica>Medley</discografica>
+ <precio>7.80</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Pavarotti Gala Concert</titulo>
+ <artista>Luciano Pavarotti</artista>
+ <pais>UK</pais>
+ <discografica>DECCA</discografica>
+ <precio>9.90</precio>
+ <anio>1991</anio>
+ </cd>
+ <cd valoracion="1">
+ <titulo>The dock of the bay</titulo>
+ <artista>Otis Redding</artista>
+ <pais>USA</pais>
+ <discografica>Atlantic</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Picture book</titulo>
+ <artista>Simply Red</artista>
+ <pais>EU</pais>
+ <discografica>Elektra</discografica>
+ <precio>7.20</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Red</titulo>
+ <artista>The Communards</artista>
+ <pais>UK</pais>
+ <discografica>London</discografica>
+ <precio>7.80</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="10">
+ <titulo>Unchain my heart</titulo>
+ <artista>Joe Cocker</artista>
+ <pais>USA</pais>
+ <discografica>EMI</discografica>
+ <precio>8.20</precio>
+ <anio>1987</anio>
+ </cd>
+</catalogo>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<etiquetaPrincipal>
+ <etiquetaHija id="1" atributo1="valorAtributo1" atributo2="valorAtributo2">
+ Texto dentro de la etiqueta hija
+ </etiquetaHija>
+ <!-- Comentario -->
+</etiquetaPrincipal>
+
--- /dev/null
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class ManejadorSax1 extends DefaultHandler{
+ public void startDocument() throws SAXException {
+ System.out.println("\nPrincipio del documento...");
+ }
+
+ public void endDocument() throws SAXException {
+ System.out.println("\nFin del documento...");
+ }
+
+ public void startElement(String uri, String localName, String name,
+ Attributes attributes) throws SAXException {
+ System.out.println("\nProcesando etiqueta...");
+ System.out.println("\tNombre: "+localName);
+
+ //Recorremos los atributos
+ System.out.println("\tProcesando "+attributes.getLength()+" atributos...");
+ for(int i=0;i<attributes.getLength();i++){
+ System.out.println("\t\tNombre: "+attributes.getQName(i));
+ System.out.println("\t\tValor: "+attributes.getValue(i));
+ }
+
+ // También podemos obtener los atributos por nombre
+ String valorId = attributes.getValue("id");
+ if(valorId!=null){
+ System.out.println("\tId: "+valorId);
+ }
+
+ }
+
+ public void characters(char[] ch, int start, int length)
+ throws SAXException {
+ String texto = String.valueOf(ch, start, length);
+ texto = texto.trim();
+ if (!texto.equals("")){
+ System.out.println("\nProcesando texto dentro de una etiqueta... ");
+ System.out.println("\tTexto: "+texto);
+ }
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String name)
+ throws SAXException {
+ System.out.println("\nFin de etiqueta...");
+ System.out.println("\tNombre: "+localName);
+ }
+
+}
\ No newline at end of file
--- /dev/null
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+
+public class Parser {
+ public static void main(String[] args) {
+ // Construimos nuestro DocumentBuilder
+ DocumentBuilder documentBuilder = null;
+ Document doc = null;
+ try {
+ documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+
+ // Procesamos el fichero XML y obtenemos nuestro objeto Document
+ try {
+ doc = documentBuilder.parse("src/xml1.xml");
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SAXException e) {
+ e.printStackTrace();
+ }
+ // Obtenemos la etiqueta raiz
+ Element elementRaiz = doc.getDocumentElement();
+ // Iteramos sobre sus hijos
+ NodeList hijos = elementRaiz.getChildNodes();
+ for(int i=0;i<hijos.getLength();i++){
+ Node nodo = hijos.item(i);
+ if (nodo instanceof Element){
+ System.out.println(nodo.getNodeName());
+ }
+ }
+
+ }
+}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<?xml-stylesheet href="catalogo.xsl" type="text/xsl"?>
+<catalogo>
+ <cd valoracion="10">
+ <titulo>Empire Burlesque</titulo>
+ <artista>Bob Dylan</artista>
+ <pais>USA</pais>
+ <discografica>Columbia</discografica>
+ <precio>10.90</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Bulería</titulo>
+ <artista>David Bisbal</artista>
+ <pais>España</pais>
+ <discografica>Vale</discografica>
+ <precio>20</precio>
+ <anio>2004</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Hide your heart</titulo>
+ <artista>Bonnie Tyler</artista>
+ <pais>UK</pais>
+ <discografica>CBS Records</discografica>
+ <precio>9.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Greatest Hits</titulo>
+ <artista>Dolly Parton</artista>
+ <pais>USA</pais>
+ <discografica>RCA</discografica>
+ <precio>9.90</precio>
+ <anio>1982</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>Still got the blues</titulo>
+ <artista>Gary Moore</artista>
+ <pais>UK</pais>
+ <discografica>Virgin records</discografica>
+ <precio>10.20</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Eros</titulo>
+ <artista>Eros Ramazzotti</artista>
+ <pais>EU</pais>
+ <discografica>BMG</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>One night only</titulo>
+ <artista>Bee Gees</artista>
+ <pais>UK</pais>
+ <discografica>Polydor</discografica>
+ <precio>10.90</precio>
+ <anio>1998</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Sylvias Mother</titulo>
+ <artista>Dr.Hook</artista>
+ <pais>UK</pais>
+ <discografica>CBS</discografica>
+ <precio>8.10</precio>
+ <anio>1973</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Maggie May</titulo>
+ <artista>Rod Stewart</artista>
+ <pais>UK</pais>
+ <discografica>Pickwick</discografica>
+ <precio>8.50</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Romanza</titulo>
+ <artista>Andrea Bocelli</artista>
+ <pais>EU</pais>
+ <discografica>Polydor</discografica>
+ <precio>10.80</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>When a man loves a woman</titulo>
+ <artista>Percy Sledge</artista>
+ <pais>USA</pais>
+ <discografica>Atlantic</discografica>
+ <precio>8.70</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Black angel</titulo>
+ <artista>Savage Rose</artista>
+ <pais>EU</pais>
+ <discografica>Mega</discografica>
+ <precio>10.90</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="4">
+ <titulo>1999 Grammy Nominees</titulo>
+ <artista>Many</artista>
+ <pais>USA</pais>
+ <discografica>Grammy</discografica>
+ <precio>10.20</precio>
+ <anio>1999</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>For the good times</titulo>
+ <artista>Kenny Rogers</artista>
+ <pais>UK</pais>
+ <discografica>Mucik Master</discografica>
+ <precio>8.70</precio>
+ <anio>1995</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Big Willie style</titulo>
+ <artista>Will Smith</artista>
+ <pais>USA</pais>
+ <discografica>Columbia</discografica>
+ <precio>9.90</precio>
+ <anio>1997</anio>
+ </cd>
+ <cd valoracion="2">
+ <titulo>Tupelo Honey</titulo>
+ <artista>Van Morrison</artista>
+ <pais>UK</pais>
+ <discografica>Polydor</discografica>
+ <precio>8.20</precio>
+ <anio>1971</anio>
+ </cd>
+ <cd valoracion="9">
+ <titulo>Soulsville</titulo>
+ <artista>Jorn Hoel</artista>
+ <pais>Norway</pais>
+ <discografica>WEA</discografica>
+ <precio>7.90</precio>
+ <anio>1996</anio>
+ </cd>
+ <cd valoracion="8">
+ <titulo>The very best of</titulo>
+ <artista>Cat Stevens</artista>
+ <pais>UK</pais>
+ <discografica>Island</discografica>
+ <precio>8.90</precio>
+ <anio>1990</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Stop</titulo>
+ <artista>Sam Brown</artista>
+ <pais>UK</pais>
+ <discografica>A and M</discografica>
+ <precio>8.90</precio>
+ <anio>1988</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Bridge of Spies</titulo>
+ <artista>T`Pau</artista>
+ <pais>UK</pais>
+ <discografica>Siren</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="6">
+ <titulo>Private Dancer</titulo>
+ <artista>Tina Turner</artista>
+ <pais>UK</pais>
+ <discografica>Capitol</discografica>
+ <precio>8.90</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="5">
+ <titulo>Midt om natten</titulo>
+ <artista>Kim Larsen</artista>
+ <pais>EU</pais>
+ <discografica>Medley</discografica>
+ <precio>7.80</precio>
+ <anio>1983</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Pavarotti Gala Concert</titulo>
+ <artista>Luciano Pavarotti</artista>
+ <pais>UK</pais>
+ <discografica>DECCA</discografica>
+ <precio>9.90</precio>
+ <anio>1991</anio>
+ </cd>
+ <cd valoracion="1">
+ <titulo>The dock of the bay</titulo>
+ <artista>Otis Redding</artista>
+ <pais>USA</pais>
+ <discografica>Atlantic</discografica>
+ <precio>7.90</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="3">
+ <titulo>Picture book</titulo>
+ <artista>Simply Red</artista>
+ <pais>EU</pais>
+ <discografica>Elektra</discografica>
+ <precio>7.20</precio>
+ <anio>1985</anio>
+ </cd>
+ <cd valoracion="7">
+ <titulo>Red</titulo>
+ <artista>The Communards</artista>
+ <pais>UK</pais>
+ <discografica>London</discografica>
+ <precio>7.80</precio>
+ <anio>1987</anio>
+ </cd>
+ <cd valoracion="10">
+ <titulo>Unchain my heart</titulo>
+ <artista>Joe Cocker</artista>
+ <pais>USA</pais>
+ <discografica>EMI</discografica>
+ <precio>8.20</precio>
+ <anio>1987</anio>
+ </cd>
+</catalogo>
--- /dev/null
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+
+public class ejercicio1 {
+ public static void main(String[] args) {
+ // Construimos nuestro DocumentBuilder
+ DocumentBuilder documentBuilder = null;
+ Document doc = null;
+ try {
+ documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+
+ // Procesamos el fichero XML y obtenemos nuestro objeto Document
+ try {
+ doc = documentBuilder.parse("src/catalogo.xml");
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SAXException e) {
+ e.printStackTrace();
+ }
+ // Obtenemos la etiqueta raiz
+ Element elementRaiz = doc.getDocumentElement();
+ // Iteramos sobre sus hijos
+ NodeList hijos = elementRaiz.getChildNodes();
+ for(int i=0;i<hijos.getLength();i++){
+ Node nodo = hijos.item(i);
+ if (nodo instanceof Element){
+ Element e = (Element) nodo;
+ System.out.println(e.getTagName() + ' ' + i);
+ }
+ }
+
+ }
+}
--- /dev/null
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+
+public class ejercicio2 {
+ public static void main(String[] args) {
+ // Construimos nuestro DocumentBuilder
+ DocumentBuilder documentBuilder = null;
+ Document doc = null;
+ try {
+ documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+
+ // Procesamos el fichero XML y obtenemos nuestro objeto Document
+ try {
+ doc = documentBuilder.parse("src/catalogo.xml");
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SAXException e) {
+ e.printStackTrace();
+ }
+ // Obtenemos la etiqueta raiz
+ Element elementRaiz = doc.getDocumentElement();
+ // Iteramos sobre sus hijos
+ NodeList hijos = elementRaiz.getChildNodes();
+ for(int i=0;i<hijos.getLength();i++){
+ Node nodo = hijos.item(i);
+ if (nodo instanceof Element){
+ Element e = (Element) nodo;
+ if (e.getTagName().equals("cd")){
+ Node tituloNode = e.getElementsByTagName("titulo").item(0);
+ String titulo = tituloNode.getFirstChild().getNodeValue();
+ String valoracion = e.getAttribute("valoracion");
+ System.out.println("titulo: " + titulo + " | valoracion: " + valoracion);
+ }
+ }
+ }
+
+ }
+}
--- /dev/null
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+
+public class ejercicio3 {
+ public static void main(String[] args) {
+ // Construimos nuestro DocumentBuilder
+ DocumentBuilder documentBuilder = null;
+ Document doc = null;
+ try {
+ documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ e.printStackTrace();
+ }
+
+ // Procesamos el fichero XML y obtenemos nuestro objeto Document
+ try {
+ doc = documentBuilder.parse("src/catalogo.xml");
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SAXException e) {
+ e.printStackTrace();
+ }
+ // Obtenemos la etiqueta raiz
+ Element elementRaiz = doc.getDocumentElement();
+ // Iteramos sobre sus hijos
+ NodeList hijos = elementRaiz.getChildNodes();
+ for(int i=0;i<hijos.getLength();i++){
+ Node nodo = hijos.item(i);
+ if (nodo instanceof Element){
+ Element e = (Element) nodo;
+ if (e.getTagName().equals("cd")){
+ Node paisNode = e.getElementsByTagName("pais").item(0);
+ String pais = paisNode.getFirstChild().getNodeValue();
+ Node artistaNode = e.getElementsByTagName("artista").item(0);
+ Node tituloNode = e.getElementsByTagName("titulo").item(0);
+ String titulo = tituloNode.getFirstChild().getNodeValue();
+ String artista = artistaNode.getFirstChild().getNodeValue();
+ if (pais.equals("USA"))
+ System.out.println("titulo: " + titulo + " | artista: " + artista);
+ }
+ }
+ }
+
+ }
+}
--- /dev/null
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class ejercicioSax1 extends DefaultHandler{
+ int etiquetas;
+ public ejercicioSax1(){
+ super();
+ etiquetas = 0;
+ }
+
+ public void startElement(String uri, String localName, String name,
+ Attributes attributes) throws SAXException {
+ if (localName.equals("cd")){
+ this.etiquetas++;
+ System.out.println(localName + ' ' + this.etiquetas);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+import java.lang.Integer;
+
+public class ejercicioSax2 extends DefaultHandler{
+ Boolean cd;
+ Boolean titulo;
+ int valoracion;
+
+ public ejercicioSax2() {
+ super();
+ this.cd = false;
+ this.titulo = false;
+ this.valoracion = 0;
+ }
+
+ public void startElement(String uri, String localName, String name,
+ Attributes attributes) throws SAXException {
+
+ if (localName.equals("cd")){
+ cd = true;
+ String valor = attributes.getValue("valoracion");
+ if(valor!=null){
+ this.valoracion = new Integer(valor);
+ }
+ }else if(localName.equals("titulo")){
+ titulo = true;
+ }
+ }
+
+ public void characters(char[] ch, int start, int length)
+ throws SAXException {
+ String texto = String.valueOf(ch, start, length);
+ texto = texto.trim();
+ if (!texto.equals("") && cd && titulo){
+ System.out.println("Titulo: " + texto + " | valoracion: " + this.valoracion);
+ }
+ }
+
+ public void endElement(String uri, String localName, String name)
+ throws SAXException {
+ if (localName.equals("cd")){
+ cd = false;
+ }else if(localName.equals("titulo")){
+ titulo = false;
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+public class ejercicioSax3 extends DefaultHandler{
+ Boolean cd;
+ Boolean titulo;
+ Boolean artista;
+ Boolean pais;
+ String tituloValor;
+ String artistaValor;
+ String paisValor;
+
+ public ejercicioSax3() {
+ super();
+ this.cd = false;
+ this.titulo = false;
+ this.artista = false;
+ this.pais = false;
+ this.artistaValor = "";
+ this.tituloValor = "";
+ this.paisValor = "";
+ }
+
+ public void startElement(String uri, String localName, String name,
+ Attributes attributes) throws SAXException {
+
+ if (localName.equals("cd")){
+ cd = true;
+ }else if(localName.equals("titulo")){
+ titulo = true;
+ }else if(localName.equals("artista")){
+ artista = true;
+ }else if(localName.equals("pais")){
+ pais = true;
+ }
+ }
+
+ public void characters(char[] ch, int start, int length)
+ throws SAXException {
+ String texto = String.valueOf(ch, start, length);
+ texto = texto.trim();
+ if (!texto.equals("") && cd){
+ if (titulo)
+ this.tituloValor = texto;
+ else if (artista)
+ this.artistaValor = texto;
+ else if (pais)
+ this.paisValor = texto;
+ }
+ }
+
+ public void endElement(String uri, String localName, String name)
+ throws SAXException {
+ if (localName.equals("cd")){
+ cd = false;
+ if (paisValor.equals("USA"))
+ System.out.println("titulo: "+ tituloValor + " | artista: " + artistaValor);
+ }else if(localName.equals("titulo")){
+ titulo = false;
+ }
+ else if(localName.equals("artista")){
+ artista = false;
+ }
+ else if(localName.equals("pais")){
+ pais = false;
+ }
+ }
+
+}
\ No newline at end of file
--- /dev/null
+import java.io.IOException;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+
+public class sax1 {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ String fichero = "src/catalogo.xml";
+
+ try {
+ // Creamos la factoria de parseadores por defecto
+ XMLReader reader = XMLReaderFactory.createXMLReader();
+ // Añadimos nuestro manejador al reader
+ // reader.setContentHandler(new ManejadorSax1());
+ reader.setContentHandler(new ejercicioSax2());
+ // Procesamos el xml de ejemplo
+ reader.parse(fichero);
+ } catch (SAXException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<etiquetaPrincipal>
+ <etiquetaHija id="1" atributo1="valorAtributo1" atributo2="valorAtributo2">
+ Texto dentro de la etiqueta hija
+ </etiquetaHija>
+ <!-- Comentario -->
+</etiquetaPrincipal>
+
--- /dev/null
+- abrir eclipse:
+ - crear proyecto
+ - nueva clase
+ - copiar plantilla
+ - importar fichero catalogo.xml
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<etiquetaPrincipal>
+ <etiquetaHija id="1" atributo1="valorAtributo1" atributo2="valorAtributo2">
+ Texto dentro de la etiqueta hija
+ </etiquetaHija>
+ <!-- Comentario -->
+</etiquetaPrincipal>
+