public class ParserAdapter
This class wraps a SAX1 <doc>Parser</doc>
Interface Parser
Deprecated. This interface has been replaced by the
SAX2 XMLReader interface,
which includes Namespace support.
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
import java.io.IOException;
import org.xml.sax.XMLFilter;
import org.xml.sax.helpers.XMLFilterImpl;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStream;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class RssSaxReader
{
public static void main (String[] args)
{
try
{
String parserClass = "org.apache.crimson.parser.XMLReaderImpl";
XMLReader reader = XMLReaderFactory.createXMLReader(parserClass);
XMLFilter filter = new XMLFilterImpl();
filter.setParent(reader);
filter.setContentHandler(new BlogReader());
URL u = new URL( "
http://www.altmuslim.com/syndicate.xml");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.connect();
InputStream inputStream = huc.getInputStream();
InputSource theblog = new InputSource(inputStream);
filter.parse(theblog);
huc.disconnect();
}
catch (IOException ioe)
{
System.out.println("IO Exception: "+ioe.getMessage());
}
catch (SAXException se)
{
System.out.println("SAX Exception: "+se.getMessage());
se.printStackTrace();
}
}
static class BlogReader extends DefaultHandler
{
StringBuffer thisText = new StringBuffer();
String title = "";
String link = "";
public void endElement (String namespaceUri, String localName, String qualifiedName)
{
if (localName.equals("title"))
{
title = thisText.toString().trim();
}
if (localName.equals("link"))
{
link = thisText.toString().trim();
}
if (localName.equals("item"))
{
System.out.println("<a href=\""+link+"\">"+title+"</a>");
}
thisText.delete(0, thisText.length());
}
public void characters (char[] ch, int start, int length)
{
String sChars;
thisText.append(ch, start, length);
}
}
}
/*
outputs for 2004-April-22
<a href="
http://www.altmuslim.com/headlines.php?id=P1218">Should US Mosques Have Minarets With Loudspeakers?</a>
<a href="
http://www.altmuslim.com/news.php?id=P1217">Other Foot Drops With Rantisi Assassination: What Happens Now?</a>
<a href="
http://www.altmuslim.com/news.php?id=P1215">Undemocratic Countries Avoid "Islam & Democracy" Conference</a>
<a href="
http://www.altmuslim.com/news.php?id=P1213">Ten Years After Rwanda, Sudan Crisis Grows Critical</a>
<a href="
http://www.altmuslim.com/opinion.php?id=P1212">Daniel Pipes: The New Voice Of Moderate Islam?</a>
<a href="
http://www.altmuslim.com/news.php?id=P1211">No Mercy: The Deportation Of Amina Silmi</a>
<a href="
http://www.altmuslim.com/news.php?id=P1210">Survey Says: American Muslims Are Liberal, Religious, Dislike Bush</a>
<a href="
http://www.altmuslim.com/news.php?id=P1209">Iraq Pot Boils Over, US Tries To Keep The Lid On</a>
<a href="
http://www.altmuslim.com/family.php?id=P1208">British Muslims Speak Out About "Loudmouths" In Their Midst</a>
<a href="
http://www.altmuslim.com/family.php?id=P1207">Generation M: Growing Up Muslim In America</a>
*/
Have a good day.
Peace be unto you.