Last active
June 11, 2017 01:56
-
-
Save alvareztech/f0c323b92201fadc1f4a1782cf6d618f to your computer and use it in GitHub Desktop.
Clase ParserXML para el tratamiento de XML desde una URL. Curso de desarrollo de aplicaciones Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.sax.Element; | |
import android.sax.EndElementListener; | |
import android.sax.EndTextElementListener; | |
import android.sax.RootElement; | |
import android.sax.StartElementListener; | |
import android.util.Xml; | |
import org.xml.sax.Attributes; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
public class ParserXML { | |
private URL url; | |
private Evento eventoActual; | |
public ParserXML(String url) { | |
try { | |
this.url = new URL(url); | |
} catch (MalformedURLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public ArrayList<Evento> parse() { | |
final ArrayList<Evento> eventos = new ArrayList<Evento>(); | |
RootElement root = new RootElement("eventos"); | |
Element item = root.getChild("evento"); | |
item.setStartElementListener(new StartElementListener() { | |
@Override | |
public void start(Attributes attributes) { | |
eventoActual = new Evento(); | |
} | |
}); | |
item.getChild("nombre").setEndTextElementListener(new EndTextElementListener() { | |
@Override | |
public void end(String s) { | |
eventoActual.setNombre(s); | |
} | |
}); | |
item.getChild("lugar").setEndTextElementListener(new EndTextElementListener() { | |
@Override | |
public void end(String s) { | |
eventoActual.setLugar(s); | |
} | |
}); | |
item.getChild("fecha").setEndTextElementListener(new EndTextElementListener() { | |
@Override | |
public void end(String s) { | |
eventoActual.setFecha(convertir(s)); | |
} | |
}); | |
item.setEndElementListener(new EndElementListener() { | |
@Override | |
public void end() { | |
eventos.add(eventoActual); | |
} | |
}); | |
try { | |
Xml.parse(getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler()); | |
} catch (Exception ex) { | |
throw new RuntimeException(ex); | |
} | |
return eventos; | |
} | |
private InputStream getInputStream() { | |
try { | |
return url.openConnection().getInputStream(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
// Método extra | |
public static Date convertir(String fechaString) { | |
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); | |
Date date = null; | |
try { | |
date = format.parse(fechaString); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment