S
Sterntaler
Unregistered / Unconfirmed
GUEST, unregistred user!
用不用的上都说声,好吧。
import java.io.StringReader;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
public class TestParser2 extends HTMLEditorKit.ParserCallback{
String outerHTML = "<html ><head ><title >Hello World! </title></head><body><h1></h1><P><font color='red' size='9pt'>ABC</font><P>CDE<BR>EFT</P></body></html>";
StringReader in = null;
HTML.Tag m_CurTag = null;
public TestParser2() throws Exception{
init();
}
public void init() throws Exception{
in = new StringReader(outerHTML);
System.out.println(outerHTML);
new ParserDelegator().parse(in, this, true);
in.close();
in = null;
}
public void handleComment(char[] data, int pos) {
System.out.println("Comment: " + new String(data));
}
public void handleText(char[] data, int pos) {
if (m_CurTag != null)
System.out.println("Tag: " + m_CurTag + "'s innerText: " + new String(data));
}
public void handleError(String msg, int pos) {
System.out.println("Error: "+msg);
}
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
System.out.println("Simple Tag: " + t);
}
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
m_CurTag = t;
System.out.print("Tag Start: " + t);
java.util.Enumeration names = a.getAttributeNames();
while (names.hasMoreElements()){
// 如果强制转换为字符串出错!
Object attrName = names.nextElement();
System.out.print(" " + attrName + "=/"" + a.getAttribute(attrName) + "/"");
}
System.out.println();
}
public void handleEndTag(HTML.Tag t, int pos) {
m_CurTag = null;
System.out.println("Tag End: " + t);
}
public static void main(String[] args) throws Exception{
new TestParser2();
}
}
import java.io.StringReader;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
public class TestParser2 extends HTMLEditorKit.ParserCallback{
String outerHTML = "<html ><head ><title >Hello World! </title></head><body><h1></h1><P><font color='red' size='9pt'>ABC</font><P>CDE<BR>EFT</P></body></html>";
StringReader in = null;
HTML.Tag m_CurTag = null;
public TestParser2() throws Exception{
init();
}
public void init() throws Exception{
in = new StringReader(outerHTML);
System.out.println(outerHTML);
new ParserDelegator().parse(in, this, true);
in.close();
in = null;
}
public void handleComment(char[] data, int pos) {
System.out.println("Comment: " + new String(data));
}
public void handleText(char[] data, int pos) {
if (m_CurTag != null)
System.out.println("Tag: " + m_CurTag + "'s innerText: " + new String(data));
}
public void handleError(String msg, int pos) {
System.out.println("Error: "+msg);
}
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
System.out.println("Simple Tag: " + t);
}
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
m_CurTag = t;
System.out.print("Tag Start: " + t);
java.util.Enumeration names = a.getAttributeNames();
while (names.hasMoreElements()){
// 如果强制转换为字符串出错!
Object attrName = names.nextElement();
System.out.print(" " + attrName + "=/"" + a.getAttribute(attrName) + "/"");
}
System.out.println();
}
public void handleEndTag(HTML.Tag t, int pos) {
m_CurTag = null;
System.out.println("Tag End: " + t);
}
public static void main(String[] args) throws Exception{
new TestParser2();
}
}