Advertisement
Guest User

contenido

a guest
Oct 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package Ej16;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.helpers.DefaultHandler;
  8.  
  9.  
  10. /**
  11.  *
  12.  * @author hhman
  13.  */
  14. public class Contenido extends DefaultHandler{
  15.         private boolean bId = false;
  16.         private boolean bNombre = false;
  17.         private boolean bDificultad = false;
  18.         private boolean bIdDepartamento = false;
  19.         private boolean bPrecio = false;
  20.         private Curso miCurso = new Curso();
  21.         private List<Curso> listaCursos = new ArrayList<>();
  22.  
  23.     public Contenido() {
  24.         super();
  25.     }
  26.    
  27.      public void startDocument() {
  28.         System.out.println("Comienzo del Documento XML");
  29.     }
  30.    
  31.     public void endDocument() {
  32.         System.out.println("Final del Documento XML");
  33.     }
  34.    
  35.     public void startElement(String uri, String nombre, String nombreC, Attributes atts) throws SAXException{
  36.         System.out.println("\tInicio Elemento: " + nombreC);
  37.         if(nombreC.equalsIgnoreCase("Curso")){
  38.              miCurso.setId(Integer.parseInt((atts.getValue("id"))));
  39.  
  40.         } else if (nombreC.equalsIgnoreCase("Nombre")){
  41.              bNombre = true;
  42.         }else if (nombreC.equalsIgnoreCase("Dificultad")){
  43.              bDificultad = true;
  44.         } else if (nombreC.equalsIgnoreCase("Precio")){
  45.              bPrecio = true;
  46.         } else if (nombreC.equalsIgnoreCase("IdDepartamento")){
  47.              bIdDepartamento = true;
  48.         }
  49.            
  50.     }
  51.    
  52.     public void endElement(String uri, String nombre, String nombreC) throws SAXException{
  53.         System.out.println("\tFin Elemento: " + nombreC);
  54.         if(nombreC.equalsIgnoreCase("curso")) {
  55.             listaCursos.add(miCurso);
  56.             miCurso = new Curso();
  57.         }
  58.     }
  59.    
  60.     public void characters(char[] ch, int inicio, int longitud) throws SAXException{
  61.         if(bNombre) {
  62.             String nombre = new String(ch, inicio, longitud).trim();
  63.             System.out.println("\t\tnombre: " + nombre);
  64.             miCurso.setNombre(nombre);
  65.             bNombre = false;
  66.         } else if (bDificultad){
  67.             String dificultad = new String(ch, inicio, longitud).trim();
  68.             System.out.println("\t\tdificultad: " + dificultad);
  69.             miCurso.setNombre(dificultad);
  70.             bDificultad = false;
  71.     }else if (bIdDepartamento){
  72.             String iddepartamento = new String(ch, inicio, longitud).trim();
  73.             System.out.println("\t\tdep: " + iddepartamento);
  74.             miCurso.setNombre(iddepartamento);
  75.             bIdDepartamento = false;
  76.     }else if (bPrecio){
  77.             String precio = new String(ch, inicio, longitud).trim();
  78.             System.out.println("\t\tPrecio: " + precio);
  79.             miCurso.setNombre(precio);
  80.             bPrecio = false;
  81.     }
  82.        
  83.     }
  84.    
  85.     public List<Curso> getListaCursos() {
  86.         return listaCursos;
  87.     }
  88.    
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement