62,623
社区成员
发帖
与我相关
我的任务
分享<html dir="ltr"><head><title>预览</title><Base href=http://192.168.0.35:8080/portal/ ><link href="/manager/HtmlEditor/editor/css/fck_editorarea.css" rel="stylesheet" type="text/css" /></head><body><p>This is a test case.</p><p>In order to show the the String which need to be deal with.</p><p>Example image<img alt="" src="C:\Documents and Settings\user\My Documents\My Pictures\Google Talk\Bear.bmp" /> and another picture <img alt="" src="C:\Documents and Settings\user\My Documents\My Pictures\Google Talk\Sun.bmp" />and <img alt="纳什:考虑加盟德州球队三强中当属火箭希望最大" src="http://i0.sinaimg.cn/ty/k/2007-12-01/U2138P6T12D3329804F44DT20071201022602.jpg" border="1" /><br /> </p></body></html>
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
/**
* 解析HTML文件中特定元素
* LINK href
* A href
* IMG src
* SCRIPT src
*
*/
public class ParseHtml {
public static void main(String args[]) {
ParseHtml ph = new ParseHtml();
try {
String filename = "e:\\TEST.txt";
BufferedReader brd = new BufferedReader(new FileReader(filename));
char[] str = new char[50000];
brd.read(str);
String sHtml = new String(str);
startParse(sHtml);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void startParse(String sHtml) {
try {
ParserDelegator parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = new Callback();
parser.parse(new StringReader(sHtml), callback, true);
} catch (Exception e) {
e.printStackTrace();
}
}
static class Callback extends HTMLEditorKit.ParserCallback {
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t.equals(HTML.Tag.IMG)) {
String src = (String) a.getAttribute(HTML.Attribute.SRC);
if(!src.trim().substring(0, 6).equals("http:/")) {
src = "image/" + src.substring(src.lastIndexOf("\\") + 1).trim();
System.out.println(src);
}
// count++;
}
// if (t.equals(HTML.Tag.LINK)) {
// String href = (String) a.getAttribute(HTML.Attribute.HREF);
// System.out.println("No." + count + " LINK src=" + href);
// count++;
// }
}
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t.equals(HTML.Tag.A)) {
String src = (String) a.getAttribute(HTML.Attribute.HREF);
System.out.println(src);
// count++;
}
if (t.equals(HTML.Tag.SCRIPT)) {
String src = (String) a.getAttribute(HTML.Attribute.SRC);
System.out.println(src);
// count++;
}
}
}
}