Html Parser

/*
* 测试对<table><tr><td></td></tr></table>的解析
*/
public void testTable() {
Parser myParser;
NodeList nodeList = null;
myParser = Parser.createParser("<body> " + "<table id=’table1′ >"
+ "<tr><td>1-11</td><td>1-12</td><td>1-13</td>"
+ "<tr><td>1-21</td><td>1-22</td><td>1-23</td>"
+ "<tr><td>1-31</td><td>1-32</td><td>1-33</td></table>"
+ "<table id=’table2′ >"
+ "<tr><td>2-11</td><td>2-12</td><td>2-13</td>"
+ "<tr><td>2-21</td><td>2-22</td><td>2-23</td>"
+ "<tr><td>2-31</td><td>2-32</td><td>2-33</td></table>"
+ "</body>", "GBK");
NodeFilter tableFilter = new NodeClassFilter(TableTag.class);
OrFilter lastFilter = new OrFilter();
lastFilter.setPredicates(new NodeFilter[] { tableFilter });
try {
nodeList = myParser.parse(lastFilter);
for (int i = 0; i <= nodeList.size(); i++) {
if (nodeList.elementAt(i) instanceof TableTag) {
TableTag tag = (TableTag) nodeList.elementAt(i);
TableRow[] rows = tag.getRows();

for (int j = 0; j < rows.length; j++) {
TableRow tr = (TableRow) rows[j];
TableColumn[] td = tr.getColumns();
for (int k = 0; k < td.length; k++) {
logger.fatal("<td>" + td[k].toPlainTextString());
}

}

}
}

} catch (ParserException e) {
e.printStackTrace();
}
}

/*
* 测试NodeVisitor的用法,遍历所有节点
*/
public void testVisitorAll() {
try {
Parser parser = new Parser();
parser.setURL("http://www.google.com");
parser.setEncoding(parser.getEncoding());

NodeVisitor visitor = new NodeVisitor() {
public void visitTag(Tag tag) {
logger.fatal("testVisitorAll() Tag name is :"
+ tag.getTagName() + " \n Class is :"
+ tag.getClass());
}
};
parser.visitAllNodesWith(visitor);
} catch (ParserException e) {
e.printStackTrace();
}
}
/*
* 测试对指定Tag的NodeVisitor的用法
*/
public void testTagVisitor() {
try {
Parser parser = new Parser(
"<head><title>dddd</title>"
+ "<link href=http://www.yeeach.com/’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "<link href=http://www.yeeach.com/’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "</head>" + "<body>"
+ "<a href=http://www.yeeach.com/’http://www.yeeach.com’>yeeach.com</a>"
+ "</body>");
NodeVisitor visitor = new NodeVisitor() {
public void visitTag(Tag tag) {
if (tag instanceof HeadTag) {
logger.fatal("visitTag() HeadTag : Tag name is :"
+ tag.getTagName() + " \n Class is :"
+ tag.getClass() + "\n Text is :"
+ tag.getText());
} else if (tag instanceof TitleTag) {
logger.fatal("visitTag() TitleTag : Tag name is :"
+ tag.getTagName() + " \n Class is :"
+ tag.getClass() + "\n Text is :"
+ tag.getText());
} else if (tag instanceof LinkTag) {
logger.fatal("visitTag() LinkTag : Tag name is :"
+ tag.getTagName() + " \n Class is :"
+ tag.getClass() + "\n Text is :"
+ tag.getText() + " \n getAttribute is :"
+ tag.getAttribute("href"));
} else {
logger.fatal("visitTag() : Tag name is :"
+ tag.getTagName() + " \n Class is :"
+ tag.getClass() + "\n Text is :"
+ tag.getText());
}
}
};
parser.visitAllNodesWith(visitor);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 测试HtmlPage的用法
*/
public void testHtmlPage() {
String inputHTML = "<html>" + "<head>"
+ "<title>Welcome to the HTMLParser website</title>"
+ "</head>" + "<body>" + "Welcome to HTMLParser"
+ "<table id=’table1′ >"
+ "<tr><td>1-11</td><td>1-12</td><td>1-13</td>"
+ "<tr><td>1-21</td><td>1-22</td><td>1-23</td>"
+ "<tr><td>1-31</td><td>1-32</td><td>1-33</td></table>"
+ "<table id=’table2′ >"
+ "<tr><td>2-11</td><td>2-12</td><td>2-13</td>"
+ "<tr><td>2-21</td><td>2-22</td><td>2-23</td>"
+ "<tr><td>2-31</td><td>2-32</td><td>2-33</td></table>"
+ "</body>" + "</html>";
Parser parser = new Parser();
try {
parser.setInputHTML(inputHTML);
parser.setEncoding(parser.getURL());
HtmlPage page = new HtmlPage(parser);
parser.visitAllNodesWith(page);
logger.fatal("testHtmlPage -title is :" + page.getTitle());
NodeList list = page.getBody();
for (NodeIterator iterator = list.elements(); iterator
.hasMoreNodes();) {
Node node = iterator.nextNode();
logger.fatal("testHtmlPage -node is :" + node.toHtml());
}
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 测试LinkBean的用法
*/
public void testLinkBean() {
Parser parser = new Parser();
LinkBean linkBean = new LinkBean();
linkBean.setURL("http://www.google.com");
URL[] urls = linkBean.getLinks();
for (int i = 0; i < urls.length; i++) {
URL url = urls[i];
logger.fatal("testLinkBean() -url is :" + url);
}
}

}

[quote]另外一种[/quote]
public class ParserTestCase extends TestCase {
private static final Logger logger = Logger.getLogger(ParserTestCase.class);

public ParserTestCase(String name) {
super(name);
}
/*
* 测试ObjectFindVisitor的用法
*/
public void testImageVisitor() {
try {
ImageTag imgLink;
ObjectFindingVisitor visitor = new ObjectFindingVisitor(ImageTag.class);
Parser parser = new Parser();
parser.setURL("http://www.google.com");
parser.setEncoding(parser.getEncoding());
parser.visitAllNodesWith(visitor);
Node[] nodes = visitor.getTags();
for (int i = 0; i < nodes.length; i++) {
imgLink = (ImageTag) nodes[i];
logger.fatal("testImageVisitor() ImageURL = "
+ imgLink.getImageURL());
logger.fatal("testImageVisitor() ImageLocation = "
+ imgLink.extractImageLocn());
logger.fatal("testImageVisitor() SRC = "
+ imgLink.getAttribute("SRC"));
}
}catch (Exception e) {
e.printStackTrace();
}
}

/*
* 测试TagNameFilter用法
*/
public void testNodeFilter() {
try {
NodeFilter filter = new TagNameFilter("IMG");
Parser parser = new Parser();
parser.setURL("http://www.google.com");
parser.setEncoding(parser.getEncoding());
NodeList list = parser.extractAllNodesThatMatch(filter);
for (int i = 0; i < list.size(); i++) {
logger.fatal("testNodeFilter() " + list.elementAt(i).toHtml());
}
} catch (Exception e) {
e.printStackTrace();
}

}
/*
* 测试NodeClassFilter用法
*/
public void testLinkTag() {
try {

NodeFilter filter = new NodeClassFilter(LinkTag.class);
Parser parser = new Parser();
parser.setURL("http://www.google.com");
parser.setEncoding(parser.getEncoding());
NodeList list = parser.extractAllNodesThatMatch(filter);
for (int i = 0; i < list.size(); i++) {
LinkTag node = (LinkTag) list.elementAt(i);
logger.fatal("testLinkTag() Link is :" + node.extractLink());
}
} catch (Exception e) {
e.printStackTrace();
}

}
/*
* 测试<link href=" text=’text/css’ rel=’stylesheet’ />用法
*/
public void testLinkCSS() {
try {
Parser parser = new Parser();
parser
.setInputHTML("<head><title>Link Test</title>"
+ "<link href=http://www.yeeach.com/’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "<link href=http://www.yeeach.com/’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "</head>" + "<body>");
parser.setEncoding(parser.getEncoding());
NodeList nodeList = null;
for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
Node node = e.nextNode();
logger.fatal("testLinkCSS()" + node.getText()+ node.getClass());

}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 测试OrFilter的用法
*/
public void testOrFilter() {
NodeFilter inputFilter = new NodeClassFilter(InputTag.class);
NodeFilter selectFilter = new NodeClassFilter(SelectTag.class);
Parser myParser;
NodeList nodeList = null;

try {
Parser parser = new Parser();
parser
.setInputHTML("<head><title>OrFilter Test</title>"
+ "<link href=http://www.yeeach.com/’/test01/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "<link href=http://www.yeeach.com/’/test02/css.css’ text=’text/css’ rel=’stylesheet’ />"
+ "</head>"
+ "<body>"
+ "<input type=’text’ value=’text1′ name=’text1′/>"
+ "<input type=’text’ value=’text2′ name=’text2′/>"
+ "<select><option id=’1′>1</option><option id=’2′>2</option><option id=’3′></option></select>"
+ "<a href=http://www.yeeach.com/’http://www.yeeach.com’>yeeach.com</a>"
+ "</body>");

parser.setEncoding(parser.getEncoding());
OrFilter lastFilter = new OrFilter();
lastFilter.setPredicates(new NodeFilter[] { selectFilter,
inputFilter });
nodeList = parser.parse(lastFilter);
for (int i = 0; i <= nodeList.size(); i++) {
if (nodeList.elementAt(i) instanceof InputTag) {
InputTag tag = (InputTag) nodeList.elementAt(i);
logger.fatal("OrFilter tag name is :" + tag.getTagName()
+ " ,tag value is:" + tag.getAttribute("value"));
}
if (nodeList.elementAt(i) instanceof SelectTag) {
SelectTag tag = (SelectTag) nodeList.elementAt(i);
NodeList list = tag.getChildren();

for (int j = 0; j < list.size(); j++) {
OptionTag option = (OptionTag) list.elementAt(j);
logger
.fatal("OrFilter Option"
+ option.getOptionText());
}

}
}

} catch (ParserException e) {
e.printStackTrace();
}
}

}
org.htmlparser.Tag org.htmlparser.Node org.htmlparser.Text org.htmlparser.Parser org.htmlparser.Remark org.htmlparser.tags.Div org.htmlparser.Attribute org.htmlparser.tags.Html org.htmlparser.tags.Span org.htmlparser.NodeFilter org.htmlparser.lexer.Page org.htmlparser.NodeFactory org.htmlparser.http.Cookie org.htmlparser.lexer.Lexer org.htmlparser.sax.Locator org.htmlparser.tags.Bullet org.htmlparser.tags.JspTag org.htmlparser.lexer.Cursor org.htmlparser.lexer.Source org.htmlparser.lexer.Stream org.htmlparser.sax.Feedback org.htmlparser.tags.BodyTag org.htmlparser.tags.FormTag org.htmlparser.tags.HeadTag org.htmlparser.tags.LinkTag org.htmlparser.tags.MetaTag org.htmlparser.nodes.TagNode org.htmlparser.sax.XMLReader org.htmlparser.tags.FrameTag org.htmlparser.tags.ImageTag org.htmlparser.tags.InputTag org.htmlparser.tags.LabelTag org.htmlparser.tags.StyleTag org.htmlparser.tags.TableRow org.htmlparser.tags.TableTag org.htmlparser.tags.TitleTag org.htmlparser.util.NodeList org.htmlparser.beans.LinkBean org.htmlparser.nodes.TextNode org.htmlparser.sax.Attributes org.htmlparser.tags.AppletTag org.htmlparser.tags.ObjectTag org.htmlparser.tags.OptionTag org.htmlparser.tags.ScriptTag org.htmlparser.tags.SelectTag org.htmlparser.util.Translate org.htmlparser.util.sort.Sort org.htmlparser.beans.BeanyBaby org.htmlparser.http.HttpHeader org.htmlparser.lexer.PageIndex org.htmlparser.tags.BulletList org.htmlparser.tags.DoctypeTag org.htmlparser.tags.HeadingTag org.htmlparser.util.NodeList$1 org.htmlparser.beans.FilterBean org.htmlparser.beans.StringBean org.htmlparser.filters.OrFilter org.htmlparser.nodes.RemarkNode org.htmlparser.scanners.Scanner org.htmlparser.tags.BaseHrefTag org.htmlparser.tags.FrameSetTag org.htmlparser.tags.TableColumn org.htmlparser.tags.TableHeader org.htmlparser.tags.TextareaTag org.htmlparser.util.ParserUtils org.htmlparser.beans.BeanyBaby$1 org.htmlparser.filters.AndFilter org.htmlparser.filters.NotFilter org.htmlparser.filters.XorFilter org.htmlparser.tags.CompositeTag org.htmlparser.tags.ParagraphTag org.htmlparser.util.IteratorImpl org.htmlparser.util.NodeIterator org.htmlparser.visitors.HtmlPage org.htmlparser.util.sort.Ordered org.htmlparser.beans.HTMLLinkBean org.htmlparser.beans.HTMLTextBean org.htmlparser.lexer.StringSource org.htmlparser.nodes.AbstractNode org.htmlparser.util.sort.Sortable org.htmlparser.filters.RegexFilter org.htmlparser.lexer.PageAttribute org.htmlparser.scanners.JspScanner org.htmlparser.scanners.TagScanner org.htmlparser.tags.DefinitionList org.htmlparser.util.NodeTreeWalker org.htmlparser.util.ParserFeedback org.htmlparser.filters.StringFilter org.htmlparser.util.FeedbackManager org.htmlparser.util.ParserException org.htmlparser.visitors.NodeVisitor org.htmlparser.filters.IsEqualFilter org.htmlparser.filters.TagNameFilter org.htmlparser.scanners.StyleScanner org.htmlparser.util.ChainedException org.htmlparser.filters.HasChildFilter org.htmlparser.http.ConnectionManager org.htmlparser.http.ConnectionMonitor org.htmlparser.scanners.ScriptDecoder org.htmlparser.scanners.ScriptScanner org.htmlparser.PrototypicalNodeFactory org.htmlparser.filters.HasParentFilter org.htmlparser.filters.LinkRegexFilter org.htmlparser.filters.NodeClassFilter org.htmlparser.lexer.InputStreamSource org.htmlparser.util.CharacterReference org.htmlparser.util.SimpleNodeIterator org.htmlparser.filters.HasSiblingFilter org.htmlparser.filters.LinkStringFilter org.htmlparser.tags.DefinitionListBullet org.htmlparser.util.CharacterReferenceEx org.htmlparser.filters.HasAttributeFilter org.htmlparser.util.DefaultParserFeedback org.htmlparser.visitors.TagFindingVisitor org.htmlparser.visitors.LinkFindingVisitor org.htmlparser.scanners.CompositeTagScanner org.htmlparser.util.EncodingChangeException org.htmlparser.visitors.UrlModifyingVisitor org.htmlparser.filters.CssSelectorNodeFilter org.htmlparser.tags.ProcessingInstructionTag org.htmlparser.visitors.ObjectFindingVisitor org.htmlparser.visitors.StringFindingVisitor org.htmlparser.visitors.TextExtractingVisitor org.htmlparser.filters.CssSelectorNodeFilter$1 org.htmlparser.parserapplications.SiteCapturer org.htmlparser.parserapplications.WikiCapturer org.htmlparser.parserapplications.LinkExtractor org.htmlparser.parserapplications.LinkExtractor$1 org.htmlparser.parserapplications.StringExtractor org.htmlparser.filters.CssSelectorNodeFilter$YesFilter org.htmlparser.parserapplications.filterbuilder.Filter org.htmlparser.filters.CssSelectorNodeFilter$AdjacentFilter org.htmlparser.parserapplications.SiteCapturer$LocalLinkTag org.htmlparser.parserapplications.SiteCapturer$LocalFrameTag org.htmlparser.parserapplications.SiteCapturer$LocalImageTag org.htmlparser.parserapplications.filterbuilder.FilterBuilder org.htmlparser.parserapplications.filterbuilder.HtmlTreeModel org.htmlparser.parserapplications.filterbuilder.SubFilterList org.htmlparser.filters.CssSelectorNodeFilter$AttribMatchFilter org.htmlparser.filters.CssSelectorNodeFilter$HasAncestorFilter org.htmlparser.parserapplications.SiteCapturer$LocalBaseHrefTag org.htmlparser.parserapplications.filterbuilder.HtmlTreeCellRenderer org.htmlparser.parserapplications.filterbuilder.wrappers.OrFilterWrapper org.htmlparser.parserapplications.filterbuilder.layouts.NullLayoutManager org.htmlparser.parserapplications.filterbuilder.wrappers.AndFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.NotFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.RegexFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.StringFilterWrapper org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager org.htmlparser.parserapplications.filterbuilder.wrappers.TagNameFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.HasChildFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.HasParentFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.NodeClassFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.HasSiblingFilterWrapper org.htmlparser.parserapplications.filterbuilder.wrappers.HasAttributeFilterWrapper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值