已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ParseIML:package rssreader.dao;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import rssreader.model.NewsFeed;public class ParseIML private static String rootNodeName; public static ListreadXMLFile(String inFile, String dirName,String type) throws JDOMException,IOException( Document Doc = null; SAXBuilder sb = new SAXBuilder(false); File fXml = new java.io.File(inFile); Doc = sb.build(fXml); /加载一个XML文件 /定义newsFeeds返回列表类型 List newsFeeds = new ArrayList(); /创建newsFeed实例 NewsFeed newsFeed = null ; /获得根节点名 rootNodeName = rootNodeName.getName(); /创建新文件夹命路径 SaveFile.createDir(dirName +/ + rootNodeName.getName() + / + type); / 获得item标签 Element eChannel = rootNodeName.getChild(channel); ListitemList=eChannel.getChildren(item); for(int i = 0; i itemList.size(); i+) org.jdom.Element item = itemList.get(i); / 创建item的Bean实例 newsFeed = newsFeed(); / 获得节点内容 String title=item.getChildText(title).trim(); String link=item.getChildText(link); String author=item.getChildText(author); String guid=item.getChildText(guid); String pudDateString=item.getChildText(pubDate); String category=item.getChildText(category); String descriptionString=item.getChildText(description); /设置节点内容 newsFeed.setTitle(title); newsFeed.setLink(link); newsFeed.setAuthor(author); newsFeed.setGuid(guid); newsFeed.setPubDate(pubDate); newsFeed.setCategory(category); newsFeed.setdescription(description); /添加内容对象 newsFeed.add(newsFeeds); /返回内容对象容器 return newsFeeds; /* * 获取的符合新闻结构的缓存对象分类写入文本文件中 * * param List * newsFeed 新闻内容缓存对象 * param String * destFileName 目标文件夹 * param String * type 新闻类型 * throws IOException */ public static void parseListToFile(List newsFeed, String destFileName, String type) throws IOException /jdk5.0新的for循环方法获得新闻内容对象 for(Iterator iterator = newsFeed.iterator(); iterator.hasNext();) / 获得新闻内容对象 NewsFeed newsFeed = iterator.next(); / 使用已经设置好了的元素 String title = newsFeed.getTitle(); String link= newsFeed.getLink(); String author= newsFeed.getAuthor(); String guid= newsFeed.getGuid(); String pubDate = newsFeed.getPubDate().trim().replaceAll(, ).replace(, ).replace(:, ); String category = newsFeed.getCategory(); String description = newsFeed.getDescription(); /设置文本文件写入内容 StringBuffer sbf = new StringBuffer(); sbf.append(主题:+ title + + t); sbf.append(链接:+ link +t); sbf.append(作者:+ author +t); sbf.append(地址:+ guid +t); sbf.append(发布时间:+ pubDate +t); sbf.append(类型:+ category +t); sbf.append(要文:+ description +t); /把写入的内容转换为char型并生成文本文件 SaveFile.writeFileByChars(destFileName + / + rootNodeName + / + type + / + pubDate + .text,sbf.toString(); package rssreader.dao;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import com.sun.java_ernal_error;import rssreader.model.NewsFeed;import javax.swing.SizeSequence;import javax.swing.event.ListSelectionListener;import javax.swing.text.html.HTMLDocument.Iterator;public class PaarseXML private static String rootNodelName;public static ListreadXMLFile(String inFile,String dirName,String type) throws JDOMException,IOException Document doc =null; SAXBuilder sb =new SAXBuilder(false); File fXml =new File(inFile); doc =sb.build(fXml); List newsFeeds =new ArrayList(); NewsFeed newsFeed=null; Element root =doc.getRootElement(); rootNodelName =root.getName(); SaveFile.createDir(dirName+/+root.getName()+/+type); Element eChannel =root.getChild(cjannel); ListitemList=eChannel.getChildren(item); for (int i=0;iitemList.size();i+) Element item=itemList.get(i); newsFeed=new NewsFeed(); String title=item.getChildText(title).trim(); String link=item.getChildText(link); String author=item.getChildText(author); String guid=item.getChildText(guid); String category=item.getChildText(category); String pubDateString=item.getChildText(pubDate); String descriptionString=item.getChildText(description); newsFeed.setTitle(title); newsFeed.setlink(link); newsFeed.setAuthor(author); newsFeed.setGuid(guid); newsFeed.setpubDate(pubDateString); newsFeed.setCategory(category); newsFeed.setDescription(descriptionString); newsFeed.add(newsFeed); return newsFeeds;public static void parseListToFile(ListnewsFeeds,String destFileName,String type) throws IOExceptionfor(Iterator=newsFeeds.iterator();iterator.hasNext();)newsFeeds newsFeed=iterator.next();String title=newsFeed.getTitle();String link =newsFeed.getlink();String author =newsFeed.getAuthor();String guid=newsFeed.getlguid();String pubDate =newsFeed.getpubDate().trim().replaceAll(,).replace(,).repalce(:,); String description=newsFeed.getDescription(); String category=newsFeed.getcategory(); StringBuffer sbf=newsFeed StringBuffer(); sbf.append(主题: + + title + + t); sbf.append(连接: + link + t); sbf.append(作者: + link + t); sbf.append(地址: + link + t); sbf.append(发布时间: + link + t); sbf.append(类型: + link + t); sbf.append(要文: + link + t); SaveFile.writeFileBYChars(destFileName+/+rootNodelName+/+type+/+pubDate+.txt,sbf.toString();private static void StringBuffer() / TODO Auto-generated method stubFrameMainpackage ponent;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.IOException;import .URLDecoder;import java.util.Vector;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.text.BadLocationException;import org.jdom.JDOMException;import rssreader.model.URL;import rssreader.service.Service;/* * 表示层面板 */public class FrameMain extends JFrame/* 版本控制标识 */private static final longserialVersionUID= 1L;/* 业务逻辑对象 */private Serviceservice;/* 网址对象容器 */private Vectorurls;/* 目标文件 */private static StringinFile;/* 主面板 */private JPanelpanel;/* 上面板 */private JPaneltopPanel;/* 下面板 */private JPanelbottomPanel;/* 新闻标题表格面板 */private JPaneltablePanel;/* 表格 */private TableNewTitleListnewsTitleTable;/* 操作栏面板 */private JPanelmenuPanel;/* 操作栏子面板 */private JPanelmenuPanel2;/* 操作栏子面板 */private JPanelmenuPanel3;/* 表格滚动条面板 */private JScrollPanetableSolPanel;/* 新闻详情滚动面板 */private JScrollPanemessageSolPanel;/* 新闻内容文本域 */private TextPaneNewsDescriptiontextPaneNewsDescription;/* 单击事件判断标识 */private booleanflag= false;/* 读取按钮 */private JButtonreadButton;/* 查询按钮 */private JButtonqueryButton;/* 新闻类型下拉框 */private JComboBoxcomboBox;/* 站点标签 */private JLabellabel;/* 全文链接标签 */private LabelLinklk;/* 显示错误信息 */private StringerrorInfo= ;/* 查询新闻标题文本框 */private JTextFieldnewstitleText;/* 主面板行数 */private final static intPANEL_ROW_NUMBER= 2;/* 主面板列数 */private final static intPANEL_CELL_NUMBER= 1;/* 主面板宽度 */private final static intPANEL_WIDTH= 780;/* 主面板高度 */private final static intPANEL_HEIGHT= 600;/* 表格面板行数 */private final static intTABLE_PANEL_ROW_NUMBER= 1;/* 表格面板列数 */private final static intTABLE_PANEL_CELL_NUMBER= 1;/* 表格区域宽度 */private final static intTABLE_VIEW_WIDTH= 750;/* 表格区域高度 */private final static intTABLE_VIEW_HEIGHT= 220;/* 主面板标题名 */private final static StringPANEL_TITLE_NAME= RSS阅读器;/* 读取按钮命名 */private final static StringREAD_BUTTON_NAME= 读取;/* 读取按钮命名 */private final static StringQUERY_BUTTON_NAME= 查询;/* 站点标签命名 */private final static StringLABEL_NAME= 站点;/* * 面板初始化 */private void init()/ 创建业务逻辑对象service = new Service();/ 创建主面板panel = new JPanel();/ 创建分面板topPanel = new JPanel();bottomPanel = new JPanel();tablePanel = new JPanel();menuPanel = new JPanel();menuPanel2 = new JPanel();menuPanel3 = new JPanel();/ 创建读取按钮readButton = new JButton(READ_BUTTON_NAME);/ (按钮名)/ 创建查询按钮queryButton = new JButton(QUERY_BUTTON_NAME);/ (按钮名)/ 创建站点标签label = new JLabel(LABEL_NAME);/ (标签名)/ 创建查询标题文本框newstitleText = new JTextField();/ 创建下拉框urls = service.initNewsFeedURLs();comboBox = new JComboBox(urls);/ 创建标题表格newsTitleTable = new TableNewTitleList();tableSolPanel = new JScrollPane(newsTitleTable);/ 创建文本域textPaneNewsDescription = new TextPaneNewsDescription();messageSolPanel = new JScrollPane(textPaneNewsDescription);/* * 主面板构造器 */public FrameMain()/ 面板初始化init();/ 设定主面板和分面板位置panel.setLayout(new GridLayout(PANEL_ROW_NUMBER, PANEL_CELL_NUMBER);/(行/ ,/ 列/ )/ 添加上,下分面板panel.add(topPanel);panel.add(bottomPanel);/ 上面板添加菜单面板,表格面板topPanel.setLayout(new BorderLayout(); / 边界布局:顶层topPanel.add(North, menuPanel);topPanel.add(Center, tablePanel);/ 下面板添加bottomPanel.setLayout(new BorderLayout(); / 边界布局:顶层bottomPanel.add(Center, messageSolPanel);/ 菜单面板添加控件menuPanel.setLayout(new BorderLayout(); / 边界布局:顶层menuPanel.add(West, new JLabel( );menuPanel.add(Center, menuPanel2);menuPanel.add(East, menuPanel3);/ 菜单子面板添加控件menuPanel2.setLayout(new BorderLayout(); / 边界布局:顶层menuPanel2.add(West, label);menuPanel2.add(Center, comboBox);menuPanel2.add(East, readButton);/ 菜单子面板添加控件menuPanel3.setLayout(new BorderLayout(); / 边界布局:顶层menuPanel3.add(West, new JLabel( );menuPanel3.add(Center, newstitleText);menuPanel3.add(East, queryButton);/ 设定新闻标题文本框属性newstitleText.setColumns(30);/ 设定表格面板样式tablePanel.setLayout(new GridLayout(TABLE_PANEL_ROW_NUMBER,TABLE_PANEL_CELL_NUMBER);/ (行,列)/ 设置表格属性newsTitleTable.setPreferredScrollableViewportSize(new Dimension(TABLE_VIEW_WIDTH, TABLE_VIEW_HEIGHT);/ (宽,高)newsTitleTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);/ 设置标题选取方式为单行模式newsTitleTable.getSelectionModel().addListSelectionListener(new CreateRow();/ 设置表格监听newsTitleTable.addMouseListener(new TableRowClick();/ 设置表格单击监听事件/ 读取按钮监听readButton.addActionListener(new ReadButton();/ 查询按钮监听queryButton.addActionListener(new QueryButton();/ 将表格放入滚动面板tablePanel.add(tableSolPanel);/ 主面板属性设定this.setSize(PANEL_WIDTH, PANEL_HEIGHT);/ (宽,高)this.setTitle(PANEL_TITLE_NAME);/ (标题名)this.setResizable(true);/ 设置固定窗口大小this.setContentPane(panel);/ 设置内容面板/* * 标题表格监听器,创建新行事件 */private class CreateRow implements ListSelectionListenerpublic void valueChanged(ListSelectionEvent e)if (flag)/ 获得选中行的值int row = newsTitleTable.getSelectedRow();/ 网址路径String link = ;try/ 调用逻辑层对象完成新闻内容显示link = service.showMessage(row, textPaneNewsDescription);catch (BadLocationException ble)/ 获得处理BadLocation异常信息errorInfo = 文档转换操作异常;finally/ 弹窗显示错误信息showError();/ 创建查看全文标签lk = new LabelLink(link);/ 内容面板添加标签textPaneNewsDescription.insertComponent(lk);/ 表格未被单击flag = false;/* * 标题表格监听器,表格单击事件 */private class TableRowClick extends MouseAdapterpublic void mousePressed(MouseEvent e)/ 表格单击flag = true;/* * 读取按钮监听器,读取选择网址事件 */private class ReadButton implements ActionListenerpublic void actionPerformed(ActionEvent e)/ 网址字符串String address = ;/ 获得下拉框选中项Object o = comboBox.getSelectedItem();/ 获得工程根目录String dir = System.getProperty(user.dir);/ 获得下拉框选中项的文件名,赋值给网址字符串address = (URL) o).getAddress();/ 获得解析文件完整路径inFile = dir + NewsfeedFiles + address;try/ 转换路径编码为UTF-8inFile = URLDecoder.decode(inFile, utf-8);/ 调用逻辑对象对文件进行读写,显示service.fileDo(inFile, newsTitleTable, textPaneNewsDescription,address.substring(0, address.lastIndexOf(.);catch (IOException ioe)/ 获得处理IO异常信息errorInfo = 系统找不到指定文件;catch (JDOMException jde)/ 获得处理JDOM异常信息errorInfo = 解析文件异常;finally/ 弹窗显示错误信息showError();/* * 查询按钮监听器,按查询条件过滤新闻标题内容 */private class QueryButton implements ActionListenerpublic void actionPerformed(ActionEvent e)/ 调用逻辑对象实现查询功能service.queryTitle(newstitleText, textPaneNewsDescription,newsTitleTable);/* * 通过弹窗显示错误信息 */private void showError()/ 通过弹出对话框显示异常信息if (!errorInfo.equals()JOptionPane.showMessageDialog(null, errorInfo);Servicepackage rssreader.service;import java.awt.Color;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Vector;import javax.swing.JTextField;import javax.swing.text.BadLocationException;import org.jdom.JDOMException;import rssreader.dao.ParseXML;import rssreader.model.NewsFeed;import rssreader.model.URL;import ponent.TableNewTitleList;import ponent.TextPaneNewsDescription;/* * 业务逻辑类 */public class Service /* * 文件读写,显示操作 * * param String * inFile 解析文件名 * param TableNewTitleList * newsTitleTable 标题列表对象 * param TextAreaNewsDescription * newsDescriptionTextArea 文本域对象 * param String * type 文件类型字段 * throws JDOMException */public void fileDo(String inFile, TableNewTitleList newsTitleTable,TextPaneNewsDescription textPaneNewsDescription, String type)throws IOException, JDOMException String dirName;/文本文件存储文件夹路径/ 文本文件保存统一根目录String dir = System.getProperty(user.dir);dirName = dir + RSSParser;/ 创建目录并解析XML文件,装入List集合List newFeeds = ParseXML.readXMLFile(inFile, dirName, type);/ 界面显示操作newsTitleTable.deleteRow();/ 删除表格原内容textPaneNewsDescription.setText();/ 删除文本域原内容textPaneNewsDescription.setListBean(newFeeds);/ 文本域获得新闻内容容器newsTitleTable.createTable(newFeeds);/ 标题表格获得新闻内容容器 / 创建文本文件,并将List中的信息写入指定路径下ParseXML.parseListToFile(newFeeds, dirName, type);/* * 获得文件地址初始方法 */public Vector initNewsFeedURLs() / 声明新闻类型对象容器Vector urls;/ 构造新闻类型实例URL url1 = new URL(腾讯 - 国际要闻, rss_newswj.xml);URL url2 = new URL(腾讯 - 国内新闻, rss_newsgn.xml);URL url3 = new URL(新浪 - 体育新闻, sports.xml);URL url4 = new URL(新浪 - 社会新闻, focus15.xml);/ 实例新闻类型对象容器urls = new Vector();/ 添加新闻类型对象到容器中urls.add(url1);urls.add(url2);urls.add(url3);urls.add(url4);/返回容器对象return urls;/* * 查询新闻标题 * param JTextField newstitleText 输入条件文本框 * param TextPaneNewsDescription textPaneNewsDescription 内容面板 * param TableNewTitleList newsTitleTable 标题列表 */public void queryTitle(JTextField newstitleText,TextPaneNewsDescription textPaneNewsDescription,Tab
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合同格式字体大小要求
- 学位申请书导师评语
- 福建农林大学研究生学位论文格式的统一要求(2025年修订)
- 临床试验脱落报告中的统计方法学要求
- 基于全生命周期理论的军工项目管理
- 毕业论文组织管理及各环节检查落实情况总结【范本模板】
- 流动人口工作存在的问题与对策
- 本科毕业论文老师评语
- 英语专业论文格式
- MBA毕业论文答辩流程-流程是怎样的-流程介绍
- 2025秋南水北调生态环保工程有限公司招聘(15人)笔试考试参考试题及答案解析
- 2025高级生命支持(ACLS)理论考核试题库及答案
- 物流新员工流程培训
- 画展展示活动策划方案
- 全国大学生职业规划大赛《财务管理》专业生涯发展展示【曾获省级一等奖】
- 2023年湘西土家族苗族自治州辅警协警招聘考试真题及一套答案详解
- 全册教案-2025-2026学年度二年级上册音乐人音版
- 主播签电子合同范本
- 专题27.4 相似三角形的性质【十大题型】(举一反三)(人教版)(原卷版)
- 高中英语非谓语动词专项训练题库
- 河南省青桐鸣大联考2025-2026学年高一上学期10月联考物理试卷(含解析)
评论
0/150
提交评论