最近在研究lucene的全文檢索,在很多地方需要解析或者說分析Html內容或者Html頁面,Lucene本身的演示程序中也提供了一個Html Parser,
但是不是純Java的解決方案.於是到處搜索,在網路上找到了一個"HTMLParser".
過幾天貼出lucene進行全文檢索的程式碼.(檢索本站的文章等).
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.ParserClass;
importClass;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.
import org.htmlparser.visitors.HtmlPage;
import org.htmlparser.visitors.TextExtractingVisitor;
import com.jscud.util.LogMan; //一個記錄類別
/日誌」 class ParseHtmlTest
{
public static void main(String[] args) throws Exception
{
String content = readFile(a); ;
test1(content);
System.out.println("================================== ==");
System.out.println("============================== ======");
test3(content);
System.out.println("========================== ==========");
test4(content);
System.out.println("====================== ==============");
test5(aFile);
System.out.println("================== ==================");
//訪問外部資源,相對慢
test5("http://www.jscud.com");
}
/ **
* 示範了Html Parse的應用.
*
*/
public static void test5(String resource) throws Exception{
Parser myParser = new Parser(resource);
//設定編碼
myParser.setEncomyParserage"); = new HtmlPage(myParser);
myParser.visitAllNodesWith(visitor);
String textInPage = visitor.getTitle();
System.out.println(textInjage);
public static void test4(String content) throws Exception
Parser myParser;
myParser = Parser.createParser(content, "GBK");HtmlPage visitor = new Hmser。 ;String。 myParser;myParser = Parser.createParser(content, "GBK");
myParser.visitAllNodesWith(visitor);
myParser.visitAllNodesWith(visitor);
myParser.visitAllNodesWith(visitor); out.println(textInPage);
}
/**
* 讀取檔案的方式來分析內容.* filePath也可以是一個Url.
** @param resource 檔案/Url
*/public static void test2(String content) throws ParserException
{
serList nodeListception
{
Parser myParser;
serList nodeListception
{
Parser myParser;
serList nodeList content, "GBK");
NodeFilter textFilter = new NodeClassFilter(TextNode.class);
NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);
/Tag/Class .class);
OrFilter lastFilter = new OrFilter();
lastFilter.setPredicates(new NodeFilter[] { textFilter, linkFilter });nodeList = myParser.parse(lastlinkFilter });
nodeList = myParser.parse(lastlinkFilter);
nodeList = myParser.parse(lastlinkFilter); toNodeArray();
for (int i = 0; i {
Node anode = (Node) nodes[i];
String line = "";
if (anode instanceof TextNode)
{
//line = textnode.toPlainTextString().trim();
line = textnode.getText();}
else if (anode s背= (LinkTag) anode;
line = linknode.getLink();
//@todo 過濾jsp標籤:可以自行實作這個函數
}
continue;
System.out.println(line);
}}
/**
* 解析普通文字節點.
*
* @param content
* @throws ParserException
*/
public static void test1(String content) throws ParserException
{
Parser myParser;
Node[]nodes = null;
myParser =ser.PcreateParser. = myParser.extractAllNodesThatAre(TextNode.class); //這裡可能會傳送異常
for (int i = 0; i TextNode textnode = (TextNode)nodes[i]; /** * File ff = new File(sFileName); sbStr.append(dataLine); { * @param astr String public static boolean isTrimEmpty(String astr) public static boolean isBlString( (null == astr) || (astr.length() == 0))
String line = textnode.toPlainTextString().trim ( );
if (line.equals(""))
繼續;
System.out.println(line);
}
}
* @param sFileName 檔案名稱
* @param sEncode String
* @return 檔案內容
*/
public static String read String sEncode)
{
StringBuffer sbStr = new StringBuffer();
try
InputStreamReader read = new InputStreamReader(new FileStream);
InputStreamReader read = new InputStreamReader(newStreamed); new BufferedReader(read);
String dataLine = "";
while (null != (dataLine = ins.readLine()))
sbStrStr.
}
ins.close();
}
LogMan.error("讀取文字檔案錯誤", e);
}
return sbStr.toString();
/**
* @return boolean
{
if ((null == astr) || (astr.length() == 0))
{
return true;
return true;
if (isBlank(astr.trim()))
{
return true;
}
return false;
}
/**
* 字串是否為空:null或長度為0.
* @param astr 來源字串.
* @return boolean
{
return true;
}
else
{
return false;
}
}
}
內容,更多相關內容請關注PHP中文網(m.sbmmt.com)!