Setting the SAX implementation

  • Thread starter Mamadou Moustapha Kante
  • Start date
M

Mamadou Moustapha Kante

Hello everybody
I'am using the org.xml.sax.helpers.ParserAdapter to read a
XML file. When I launch my program, the jvm said that I haven't
set the property org.xml.sax.xxx . I just use the Crimson implementation
from de Sun jdk, but how to invocate it ?

Alex...

[PS] Sorry fro the frenglish...
 
M

mromarkhan

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.
 
M

Mamadou Moustapha Kante

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.
thanks a lot.
 
A

Andrew Thompson

thanks a lot.

Could you do the rest of us a favour by
deleting the text not needed, when you reply?

It would help keep the bandwidth down
for folks that are not on broadband.

It would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top