package com.yourcompany.dao;
import java.io.*;
import java.net.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
public class Test extends DefaultHandler {
// How many RSS news items should we load before stopping.
private int maximumResults =7;
/*
* How many elements should we allow before stopping the parse this stops
* giant files from breaking the server.
*/
private static final int MAX_ELEMENTS = 500;
// Keep track of the current element count.
private int ecount = 0;
// Keep track of the current news item count.
private int rcount = 0;
private String Url = "";// Url to parse.
// String to store parsed data to.
private String output = "";
// Current string being parsed.
private String currentText = "";
// Current RSS News Item.
private NewsItem NI = null;
// ArrayList of all current News Items.
private ArrayList News = new ArrayList();
// Has the RSS feed's description been set yet?
boolean dSet = false;
// Constructor.
public Test(String Url, int maximumResults) {
super();
this.Url = Url;
this.maximumResults = maximumResults;
}
/**
* Returns an HTML representation of the news feed being parsed.
*/
public synchronized String parse() {
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(this);
xr.setErrorHandler(this);
URL u = new URL(Url);
URLConnection UC = u.openConnection();
/*
* If we don't set the user-agent property sites like Google won't
* let you access their feeds.
*/
//UC.setRequestProperty("User-agent", "www.plink-search.com");
InputStreamReader r = new InputStreamReader(UC.getInputStream());
xr.parse(new InputSource(r));
} catch (Exception e) {
}
// Output all the parsed news items as HTML.
for (int i = 0; i < News.size(); i++) {
output += ((NewsItem) News.get(i)).toString();
}
return (output);
}
// //////////////////////////////////////////////////////////////////
// Event handlers.
// //////////////////////////////////////////////////////////////////
// Called when the XML file begins.
public void startDocument() {
}
// Called when the end of the XML file is reached.
public void endDocument() {
/*
* If we have a partially parsed news item throw it into our array.
*/
if (NI != null) {
rcount++;
News.add(NI);
}
}
// Called when we begin parsing the XML file.
public void startElement(String uri, String name, String qName,
Attributes atts) throws SAXException {
// qName contains the non-URI name of the XML element.
if (qName.equals("item")) {
if (NI != null) {
// We've fetched another news item.
rcount++;
// Add it to our ArrayList.
News.add(NI);
if (rcount == maximumResults) {
// Maximum results have been reached.
throw new SAXException("\nLimit reached.");
}
}
// Create a new NewsItem to add data to.
NI = new NewsItem();
}
}
// We've reached the end of an XML element.
public void endElement(String uri, String name, String qName)
throws SAXException {
if (qName.equals("title") && NI != null) {// Are we parsing a news item?
NI.setTitle(currentText);
} else if (qName.equals("link") && NI != null)
NI.setURL(currentText);
// Make sure we don't attempt to parse too long of a document.
currentText = "";
ecount++;
if (ecount > MAX_ELEMENTS)
throw new SAXException("\nLimit reached");
}
// Parse characters from the current element we're parsing.
public void characters(char ch[], int start, int length) {
for (int i = start; i < start + length; i++) {
currentText += ch[i];
}
}
// Testing main method.
public static void main(String args[]) {
Test MyRSSParser = new Test("http://blog.hiredmyway.com/feed", 20);
System.out.println(MyRSSParser.parse());
}
}
- 1
- 2
前往页