Hola!
Tengo implementado un parseador sax par aun documento xml.
El caso es que no se como integrarlo en un programa a parte.
Adjunto el codigo de mi parseador, aunq es un poco largo
Código:
public class SaxParser {
public void lee(String url){
try{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse(url, new LibraryXMLReader() );
}catch(ParserConfigurationException e){
System.err.println("error de parseo");
}catch(SAXException e2){
System.err.println("error de sax : " + e2.getStackTrace());
} catch (IOException e3) {
// TODO Auto-generated catch block
System.err.println("error de io : " + e3.getMessage() );
}
}
public static void main(String[] args){
SaxParser lector = new SaxParser();
lector. lee("rssowl.xml");
}
//---------------------------------------------------------------------------------
private class LibraryXMLReader extends DefaultHandler {
String contenido=""; // cadena para almacenar el contenido de un tag
public void startDocument() throws SAXException{
System.out.println("Estoy al principio del documento");
}
public void endDocument()throws SAXException{
System.out.println("Estoy al final del documento");
}
//Elemento nuevo
public void startElement(String uri, String localName, String qName, Attributes attributes) {
// Get the number of attribute
int length = attributes.getLength();
//Creo un array en el que iré almacenando todas las url de los enlaces
ArrayList<String> url = new ArrayList<String>();
// Process each attribute
for (int i=0; i<length; i++) {
// Get names and values for each attribute
String name = attributes.getQName(i);
String value = attributes.getValue(i);
if(name == "xmlUrl"){
System.out.println("Atributo :" + value);
url.add(value);
}
}
}
public void characters(char buf[], int offset, int len)
throws SAXException
{
contenido = new String(buf, offset, len);
}
//Fin del elemento
public void endElement(String uri, String localName, String qName) {
}
}
}
como el resto del programa, tambien tiene un main, me da error. querría que esto me devolviera al programa principal el array lisy url.
Un saludo y grcias de antemano.