Java使用HtmlParse提取标签中的值
☆代码示例:
代码块语法遵循标准markdown代码,例如:
package cas;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.StringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
/**
* Html 中的body体中提取出Img标签中的src值
*
* @author XY
*
*/
public class CASHtmlImgConvert {
public static void main(String[] args) {
//演示
String[] oldSrcPath=changeImgSrc("<img alt=\"\" src=\"http://www.czb8688.com/attached/image/20160116/20160116141455_775.jpg\" />");
if(oldSrcPath!=null){
for(String str:oldSrcPath){
System.out.println(str);
}
}
}
public static boolean isEmpty(String str){
if(str!=null&&(!str.equals("")))
return false;
else
return true;
}
/**
*
* @param htmlPath 本地的html路径 或者body
*/
private static String[] changeImgSrc(String htmlPath)
{ StringBuilder oldSrcPath = new StringBuilder();
try {
Parser parser = new Parser(htmlPath);
//标签名过滤器
NodeFilter filter = new TagNameFilter ("img");
NodeList nodes = parser.extractAllNodesThatMatch(filter);
Node eachNode = null;
ImageTag imageTag = null;
if (nodes != null)
{
// 遍历所有的img节点
for (int i = 0; i < nodes.size(); i++)
{
eachNode = (Node)nodes.elementAt(i);
if (eachNode instanceof ImageTag)
{
imageTag = (ImageTag)eachNode;
// 获得html文本的原来的src属性
String path=imageTag.getAttribute("src");
if(path.startsWith(""))
path="http://www.czb8688.com"+path;
oldSrcPath .append(path+",");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
String str=oldSrcPath.toString();
//返回图片数组
return str.substring(0,str.length()-1).split(",");
}
}