最近做项目用到了nekohtml+xpath
nekohtml将HTML文档解析为标准的DOM,再用XPath从DOM中抽取想要的结点。
nekohtml比起htmlparser还真是简单,但是简单就意味着不透明,出了错就不知是什么引起的,这一点真让人受不了。
下面先说说nekohtml+xpath的使用注意事项:
1.nekohtml会自动将html的标签转化为大写,因此写XPath时要用大写
//div 错误
//DIV 正确
2.用XPath处理时标签前要加命名空间,否则找不到结点,这里就要用到NamespaceContext这个类
class MyNamespaceContext implements NamespaceContext {
private String ns;
public MyNamespaceContext(String ns) {
this.ns = ns;
}
public String getNamespaceURI(String prefix) {
// TODO Auto-generated method stub
if (prefix == null) {
throw new NullPointerException("Null prefix");
} else if (prefix.equals("pre")) {
return this.ns;
} else if (prefix.equals("xml")) {
return XMLConstants.XML_NS_URI;
}
return XMLConstants.XML_NS_URI;
}
@Override
public String getPrefix(String namespaceURI) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator getPrefixes(String namespaceURI) {
// TODO Auto-generated method stub
return null;
}
}
实例化XPath,并设置命名空间
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new MyNamespaceContext("http://www.w3.org/1999/xhtml"));
XPathExpression expr = xpath.compile("//pre:TITLE");
最后推介一个firefox的XPath插件--XPath checker
附近上源文件
分享到:
相关推荐
采用nekohtml补全html到xhtml,结合dom,运用dom4j,支持xpath,强大的网抓工具!
nekohtml-1.9.14源码及jar包
NULL 博文链接:https://tianhewulei.iteye.com/blog/629704
NekoHTML技术预研说明,如何进行HTML页面解析,网页信息抽取
NekoHTML is written using the Xerces Native Interface (XNI) that is the foundation of the Xerces2 implementation. This enables you to use the NekoHTML parser with existing XNI tools without ...
NULL 博文链接:https://thrillerzw.iteye.com/blog/1924229
html解析器nekohtml-1.9.13.zip
NekoHTML学习笔记.doc
NekoHTML的相关用法以及代码包,以及详细的用法和实例。。。。。
NekoHTML是一个简单地HTML扫描器和标签补偿...NekoHTML能增补缺失的父元素、自动用结束标签关闭相应的元素,以及不匹配的内嵌元素标签。NekoHTML的开发使用了Xerces Native Interface (XNI),后者是Xerces2的实现基础。
NekoHTML是一个Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能够扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档 过程中常...
NeKoHTML:用于java的html编辑。此文件是NeKoHTML V1.9.21版本的Java包合集。
nekohtml.jar 解析html的jar包 非常的实用
nekohtml.jar nekohtmlSamples.jar
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的...
NULL 博文链接:https://rensanning.iteye.com/blog/1551831
nekoHtml 1.9.19 加 source 源码 html分析jar
nekohtml-1.9.18_
nekohtml-0.9.4.jar nekohtml-0.9.4.jar nekohtml-0.9.4.jar