用正则表达式,参见java.util.regex包
解决方案 »
- 为什么用浏览器访问这个url能访问两个cookie,而用httpclient只给我返回一个
- TOMCAT和JIRA端口冲突问题
- 用java代码如何区分一个字符串里边有哪些是中文,哪些是英文?
- 如何显示BMP格式文件
- struts配置SQLSERVER数据源启动TOMCAT时出错
- 多选select的name传值问题
- 谁有Struts 的APIs Doc,请给我一份,谢谢。
- jive中的beanInfo?~~~还有什么cvs
- 如何搭建jsp运行环境?用什么开发工具较好?(新手)来者有分!!!
- 各位仁兄,谁有MasteringJavabeans的例子程序源码?
- 有哪位老兄知道怎样用jsp实现cookie跨多域访问?(100分)
- 使用连接池连接IBM DB2数据库
我下载了htmlparser 但是不知道怎么用 请问有什么例子吗?
里面的参数怎么办呢
下面是一个例子给你看看:
import javax.swing.text.html.HTMLEditorKit$ParserCallback;
import javax.swing.text.html.*;
import javax.swing.text.*;
import com.pdcss.debug.*;
import java.util.*;
import javax.swing.text.html.parser.*;
import java.io.*;
import java.net.*;
import com.pdcss.xml.*;
import com.pdcss.debug.DebugOut;
public class HTMLToXML {
/**
* document对象
*/
org.w3c.dom.Document newDoc;
/**
* 根结点对象
*/
private org.w3c.dom.Element root; /**
* 是否忽略<img>标签
*/
private boolean ignoreImg=true; /**
* 设置是否忽略按钮标签
*/
private boolean ignoreButton=true; /**
* 文件路径
*/
private String filePath; /**
* 是否调用了函数parse()
*/
private boolean isParse=false; /**
* 处理结果可接受的html tagname,如果不在这个表里面则忽略掉这个元素
*/
private ArrayList acceptedTags; public HTMLToXML(String filePath) {
this.filePath=filePath;
newDoc = XmlParser.buildNewDocument();
root= newDoc.createElement("HTMLToXML");
newDoc.appendChild(root); //增加可接受的元素的tag
acceptedTags=new ArrayList(5);
acceptedTags.add(HTML.Tag.INPUT);
acceptedTags.add(HTML.Tag.TEXTAREA);
acceptedTags.add(HTML.Tag.SELECT);
} /**
* 开始解析
*/
private void parse(){
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = new MyParserCallback();
try {
URL u = new File(filePath).toURL();
InputStream in = u.openStream();
InputStreamReader reader = new InputStreamReader(in);
parser.parse(reader, callback, false);
reader.close();
}
catch (IOException e) {
}
isParse=true; } /**
* 返回创建好的Document对象
* @return
*/
public org.w3c.dom.Document getXmlDocument(){
if(isParse==false){
parse();
isParse=true;
}
return this.newDoc;
} /**
* 返回标准的xml字符串
* @return
*/
public String getXmlStr(){
if(isParse==false){
parse();
isParse=true;
}
return XmlParser.elementToString(root);
} /**
* 设置是否忽略img标签
* @param isIgnore
*/
public void setIgnoreImg(boolean isIgnore){
ignoreImg=isIgnore;
if(ignoreImg==true){
acceptedTags.remove(HTML.Tag.IMG);
}else{
if(!acceptedTags.contains(HTML.Tag.IMG)){
acceptedTags.add(HTML.Tag.IMG);
}
}
} public boolean getIgnoreImg(){
return ignoreImg;
} /**
* 设置是否忽略button标签
* @param isIgnore
*/
public void setIgnoreButton(boolean isIgnore){
ignoreButton=isIgnore;
}
private class MyParserCallback extends HTMLEditorKit.ParserCallback {
public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes, int position) {
handleTag(tag, attributes, position);
} public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attributes, int position) {
handleTag(tag, attributes, position);
} private void handleTag(HTML.Tag tag, MutableAttributeSet attributes, int position){
if(!acceptedTags.contains(tag)){
return;
}
if(ignoreButton==true){
if (tag == HTML.Tag.INPUT) {
String type = attributes.getAttribute(HTML.Attribute.TYPE).toString();
//不是按钮
if (type != null){
if (type.equalsIgnoreCase("button") || type.equalsIgnoreCase("submit") || type.equalsIgnoreCase("reset")) {
return;
}
}
}
}
org.w3c.dom.Element newElement = newDoc.createElement(tag.toString());
Enumeration enum = attributes.getAttributeNames();
Object attributeName;
while (enum.hasMoreElements()) {
attributeName = enum.nextElement();
newElement.setAttribute(attributeName.toString(), attributes.getAttribute(attributeName).toString());
}
root.appendChild(newElement); }
}}
可是偶要用的是htmlparser拉
8是这个