javax w3c 网页解析(一)
来源:互联网 发布:双网络设置 编辑:程序博客网 时间:2024/06/10 14:09
package test;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class javahtml {
public static void main(String[] args)
throws Exception
{
EditorKit kit = new HTMLEditorKit();
Document doc = kit.createDefaultDocument();
// The Document class does not yet handle charset's properly.
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
// Create a reader on the HTML content.
Reader rd = getReader("http://hexun.com/kangojian/default.html");
// Parse the HTML.
kit.read(rd, doc, 0);
// The HTML text is now stored in the document
HTMLDocument.Iterator it = ((HTMLDocument) doc).getIterator(HTML.Tag.A);
while(it.isValid())
{
SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();
String href = (String)s.getAttribute(HTML.Attribute.HREF);
System.out.println(href);
it.next();
}
}
// Returns a reader on the HTML data. If 'uri' begins
// with "http:", it's treated as a URL; otherwise,
// it's assumed to be a local filename.
static Reader getReader(String uri)
throws IOException
{
// Retrieve from Internet.
if (uri.startsWith("http:"))
{
HttpURLConnection conn = (HttpURLConnection) new URL(uri).openConnection();
return new InputStreamReader(conn.getInputStream());
}
// Retrieve from file.
else
{
return new FileReader(uri);
}
}
}
- javax w3c 网页解析(一)
- javax w3c 网页解析(二)
- w3c网页
- 互联网通信-HttpClient、XML解析(W3C)
- javascript 学习(一) w3c 标准
- w3c学习php笔记(一)
- DIV+CSS网页布局(XHTML-W3C标准)
- 网页在线验证工具(W3C国际标准验证)
- w3c.dom 解析xml
- w3c解析xml
- 前端基本知识(一):W3C标准&&冒泡事件,捕获事件,W3C DOM对象模型,对比分析
- W3C网页标准与优势
- <a>标签的target解析(参考w3c)
- 初探JDK6 javax.script (javascript解析)
- 利用Python抓取和解析网页(一)
- 利用Python抓取和解析网页(一)
- 利用Python抓取和解析网页(一)
- Python之HTML的解析(网页抓取一)
- 调用 ImageMagick 的PHP函数中文文档
- Web 程序中一定要常用Catch
- java 问题
- 修改C/C++的单行注释(//)为多行注释(/**/)的Perl代码
- DIV+CSS设计时IE6、IE7、FF 与兼容性有关的特性
- javax w3c 网页解析(一)
- ComOne中webchart控件中文说明
- DIV+CSS必须考虑的浏览器兼容的技巧
- Flex与JavaScript的交互
- BREW 应用程序下载器
- TextBox限制只允许输入半角数字
- Linux操作系统下VI编辑器常用命令介绍
- show processlist
- 关于DB2对其sql语句进行长度限制的设置语句