⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rssparser.java

📁 Java+XML写的RSS阅读器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			RssParser.status.setText(curNode.name+" 已经删除");
			curNode.removeFromParent();
			try{
				RssParser.pTree.tree.updateUI();
			}catch(Exception ec){}
			try{
				//更新到频道定义文件
				XmlProcessor.opmlWrite(RssParser.pTree.tree,"opml.xml");
			}catch(Exception ex2){
				RssParser.status.setText(ex2.getMessage());
			}
		}
	}
	void showRss(ActionEvent e){//显示RSS
		RssParser.pTable.tableModel.setRowCount(0);
		XmlProcessor.rssRead(curNode,RssParser.pTree.tree);
	}
	void showAll(ActionEvent e){//显示全部RSS
		RssParser.pTable.tableModel.setRowCount(0);
		XmlProcessor.rssReadAll(RssParser.pTree.tree);
	}
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////行为层

class XmlProcessor{//聚合操作XML的方法,方便统一调用
	//opml方法保证树和频道定义文件同构
	static void opmlWrite(JTree tree,String filename){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;

		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException pce) {
			RssParser.status.setText(pce.getMessage());
		}

		Document doc =  db.newDocument();
		Element root = doc.createElement("opml");
		Element head = doc.createElement("head");
		Element body = doc.createElement("body");
		
		root.setAttribute("version","1.1");
		doc.appendChild(root);
		root.appendChild(head);
		root.appendChild(body);

		DefaultTreeModel treeModel=(DefaultTreeModel)tree.getModel();
		RSSNode treeModelRoot=(RSSNode)treeModel.getRoot();
		int len=treeModel.getChildCount(treeModelRoot);
		for(int i=0;i<len;i++){
			RSSNode t=(RSSNode)treeModel.getChild(treeModelRoot,i);
			Element outline=doc.createElement("outline");
			outline.setAttribute("text" , t.name);
			outline.setAttribute("xmlUrl" , t.url.toString());
			body.appendChild(outline);
		}

		try{
			FileOutputStream outStream = new FileOutputStream(filename);
			OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
			((XmlDocument) doc).write(outWriter, "GB2312");
			outWriter.close();
			outStream.close();
		}catch(Exception ex){
			RssParser.status.setText(ex.getMessage());
		}

	}
	static void opmlRead(RSSNode top,String filename){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException pce) {
			RssParser.status.setText(pce.getMessage());
		}
		Document doc = null;
		try {
			doc = db.parse(filename);
			Element root = doc.getDocumentElement();
			NodeList chs = root.getElementsByTagName("body");
			Element body = (Element) chs.item(0);
			NodeList items = body.getElementsByTagName("outline");
			for (int i = 0; i < items.getLength(); i++) {
				Element R=(Element) items.item(i);
				RSSNode	rss= new RSSNode(R.getAttribute("text"));
				try{
					rss.url=new URL(R.getAttribute("xmlUrl"));
				}catch(Exception e){}
				rss.type=1;
				top.add(rss);
			}
		} catch (DOMException dom) {
			RssParser.status.setText(dom.getMessage());
		} catch (Exception ioe) {
			RssParser.status.setText(ioe.getMessage());
		}
	}
	static void rssRead(RSSNode curNode,JTree tree){
		curNode.setUserObject("("+curNode.name+")");
		try{
			RssParser.pTree.tree.updateUI();
		}catch(Exception ec){}
		RssParser.status.setText(curNode.name+" updating......");
		RssThread rt = new RssThread(curNode,tree);
		rt.start();
	//	RssParser.pTable.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
	}
	static void rssReadAll(JTree tree){
		DefaultTreeModel treeModel=(DefaultTreeModel)tree.getModel();
		RSSNode treeModelRoot=(RSSNode)treeModel.getRoot();
		int len=treeModel.getChildCount(treeModelRoot);
		for(int i=0;i<len;i++){
			RSSNode t=(RSSNode)treeModel.getChild(treeModelRoot,i);
			t.setUserObject("("+t.name+")");
			try{
				tree.updateUI();
			}catch(Exception ec){}
			RssThread rt = new RssThread(t,tree);
			rt.start();
		}
	}
}

class RSSNode extends DefaultMutableTreeNode{//扩展树节点类为RSS频道定义节点
	public int type;//0=root;1=rss;2=temp
	public String name;
	public URL url;
	public RSSNode(String s){
		super(s);
		this.name=s;
	}
}

class RssThread extends Thread{//扩展线程类为RSS更新线程
	RSSNode curNode;
	JTree tree;
	public RssThread(RSSNode curNode,JTree tree){
		super();
		this.curNode=curNode;
		this.tree=tree;
	}
	public void run(){
		URLConnection hpCon=null;
		try{//使用URL连接网络
			hpCon=curNode.url.openConnection();
		//	hpCon.connect();
		}catch(IOException ex){
			RssParser.status.setText(ex.getMessage());
		}
		InputStream inputStream=null;
		try{
			hpCon.connect();	
			inputStream=hpCon.getInputStream();//;//curNode.url.openStream()
		/*	BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
			String tmp;
			while((tmp=reader.readLine())!=null)System.out.println(tmp);*/
			
			
			//通过流读RSS所在异地xml文件
			readXMLFile(inputStream);
		}catch (IOException ex){
			RssParser.status.setText(ex.getMessage());
		}catch(Exception ex2){
			RssParser.status.setText(ex2.getMessage());
		}

		RssParser.pTable.table.setModel(RssParser.pTable.tableModel);

		//对输出结果作出响应
		if(RssParser.pTable.tableModel.getRowCount()>1)
			RssParser.status.setText(curNode.name+" done!");
		else
			RssParser.status.setText(curNode.url+" is not ready!");
	}
	
	//Rss解析的核心方法,适用xml文档定义模型XML DOM
	public void readXMLFile(InputStream inputStream) throws Exception {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;

		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException pce) {
			RssParser.status.setText(pce.getMessage());
		}
		Document doc = null;
		try {
			doc = db.parse(inputStream);
		} catch (DOMException dom) {
			RssParser.status.setText(dom.getMessage());
		} catch (IOException ioe) {
			RssParser.status.setText(ioe.getMessage());
		}
		Vector cell;
		Vector row=new Vector();
		
		Element root = doc.getDocumentElement();
		NodeList chs = root.getElementsByTagName("channel");//rss2.0
		Element ch = (Element) chs.item(0);
		Element _title = (Element)ch.getElementsByTagName("title").item(0);
		curNode.name=((Text)_title.getFirstChild()).toString();
		//更新树中节点名称为频道名称
		curNode.setUserObject(curNode.name);
		try{
		//	Thread.currentThread().yield();
		/////////////////////
		/////////////////////
		RssParser.pTree.tree.updateUI();
		//	tree.updateUI();////此处抛出的异常竟然不能捕获!		/////////////////////
		/////////////////////
		}catch(NullPointerException ep){
		}catch(InternalError eu){
		}catch(Exception ec){}

		//获取频道消息列表
		NodeList items = ch.getElementsByTagName("item");

		//遍历列表
		for (int i = 0; i < items.getLength(); i++) {
			cell=new Vector();
			
			Element R=(Element) items.item(i);
			NodeList title = R.getElementsByTagName("title");
			if (title.getLength() == 1) {
				Element e = (Element) title.item(0);
				Text t = (Text) e.getFirstChild();
				cell.add(t);
			}else{cell.add("");}
			NodeList time = R.getElementsByTagName("pubDate");
			if (time.getLength() == 1) {
				Element e = (Element) time.item(0);
				Text t = (Text) e.getFirstChild();
				cell.add(t);
			}else{
				time = R.getElementsByTagName("dc:date");
				if (time.getLength() == 1) {
					Element e = (Element) time.item(0);
					Text t = (Text) e.getFirstChild();
					cell.add(t);
				}else{cell.add("");}
			}
			NodeList description = R.getElementsByTagName("description");
			if (description.getLength() == 1) {
				Element e = (Element) description.item(0);
				Text t = (Text) e.getFirstChild();
				cell.add(t);
			}else{cell.add("");}

			//将取出的 标题、时间、摘要放入向量
			cell.add(curNode.name);
			//向表中压栈
			RssParser.pTable.tableModel.insertRow(0,cell);
		}
		//更新频道定义文件
		XmlProcessor.opmlWrite(RssParser.pTree.tree,"opml.xml");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -