📄 xmldictnumconuterwithsax.java
字号:
/* * @(#)XmlDictNumConuterWithSAX.java. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package ynu.edu.cn.sei.stoneagedict.util;import java.io.File;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import ynu.edu.cn.sei.stoneagedict.util.exception.NullWordMarkException;/** * 用sax解析xml词库文件,统计xml词库文件中的单词数量. * @author zy * @version 1.0.0.0, Mar 18, 2008 */public class XmlDictNumConuterWithSAX { private SAXParserFactory saxFactory; private SAXParser saxParser; private String filePath = null; private String wordMark = null; /** * 带参数的构造器 * @param filePath * xml文件的路径或url地址 * @param wordMark * 目标文件的单词开头的标记 */ public XmlDictNumConuterWithSAX(String filePath, String wordMark) { this.filePath = filePath; this.wordMark = wordMark; } public int getNum() throws NullWordMarkException{ File file = new File(this.filePath); XMLCounterHandler xmlHandler = new XMLCounterHandler(); if(this.wordMark == null){ throw new NullWordMarkException("单词标记为空!"); } xmlHandler.setWordMark(this.wordMark); this.saxFactory = SAXParserFactory.newInstance(); this.saxFactory.setNamespaceAware(true); try { this.saxParser = this.saxFactory.newSAXParser(); } catch (ParserConfigurationException ex) { ex.printStackTrace(); } catch (SAXException ex) { ex.printStackTrace(); } try { this.saxParser.parse(file, xmlHandler); } catch (SAXException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return xmlHandler.getNum(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -