`

解析HTML工具Jsoup的封装类

阅读更多
自己封装了使用jsoup解析HTML文件的封装工具类,欢迎大家多提点意见。

package util;

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class JsoupParserUtil {

/**
* 根据元素id获取HTML元素
* @param document
* @param id
* @return
*/
public static Element getElementById(Document document,String id)
{
Element element=null;
if(document!=null&&id!=null&&!"".equals(id.trim()))
{
element=document.getElementById(id);
}
return element;
}

/**
* 根据id获取HTML元素
* @param element
* @param id
* @return
*/
public static Element getElementById(Element element,String id)
{
Element resultElement=null;
if(element!=null)
{
resultElement=element.getElementById(id);
}
return resultElement;
}

/**
* 根据元素标签(tagName)获取HTMl元素
* @param document
* @param tagName
* @return
*/
public static Elements getElementsByTagName(Document document,String tagName)
{
Elements elements=null;
if(document!=null&&tagName!=null&&!"".equals(tagName))
{
elements=document.getElementsByTag(tagName);
}
return elements;
}

/**
* 根据元素标签(tagName)获取HTMl元素
* @param element
* @param tagName
* @return
*/
public static Elements getElementsByTagName(Element element,String tagName)
{
Elements resultElements=null;
if(element!=null&&tagName!=null&&!"".equals(tagName))
{
resultElements=element.getElementsByTag(tagName);
}
return resultElements;
}

/**
* 根据className(样式名称)获取HTML元素集合
* @param document
* @param className
* @return
*/
public static Elements getElementsByClassName(Document document,String className)
{
Elements elements=null;
if(document!=null&&className!=null&&!"".equals(className.trim()))
{
elements=document.getElementsByClass(className);
}
return elements;
}

/**
* 根据className(样式名称)获取HTML元素集合
* @param element
* @param className
* @return
*/
public static Elements getElementsByClassName(Element element,String className)
{
Elements resultElements=null;
if(element!=null&&className!=null&&!"".equals(className))
{
resultElements=element.getElementsByClass(className);
}
return resultElements;
}
/**
* 根据元素是否具有属性元素key返回元素集合
* @param document
* @param attributeNameKey 元素属性key值
* @return
*/
public static Elements getElementsByAttributeNameKey(Document document,String attributeNameKey)
{
Elements elements=null;
if(document!=null&&attributeNameKey!=null&&!"".equals(attributeNameKey))
{
elements=document.getElementsByAttribute(attributeNameKey);
}
return elements;
}

/**
* 根据元素是否具有属性元素key返回元素集合
* @param element
* @param attributeNameKey 元素属性key值
* @return
*/
public static Elements getElementsByAttributeNameKey(Element element,String attributeNameKey)
{
Elements resultElements=null;
if(element!=null&&attributeNameKey!=null&&!"".equals(attributeNameKey))
{
resultElements=element.getElementsByAttribute(attributeNameKey);
}
return resultElements;
}
/**
* 根据元素是否具有属性元素key并且key对应的值为value获取元素集合
* @param document
* @param attributeKey
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeNameValue(Document document,String attributeKey,String attributeValue)
{
Elements elements=null;
if(document!=null&&attributeKey!=null&&!"".equals(attributeKey.trim())&&attributeValue!=null&&!"".equals(attributeValue.trim()))
{
elements=document.getElementsByAttributeValue(attributeKey, attributeValue);
}
return elements;
}
/**
* 根据元素是否具有属性元素key并且key对应的值为value获取元素集合
* @param element
* @param attributeKey
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeNameValue(Element element,String attributeKey,String attributeValue)
{
Elements resultElements=null;
if(element!=null&&attributeKey!=null&&!"".equals(attributeKey.trim())&&attributeValue!=null&&!"".equals(attributeValue.trim()))
{
resultElements=element.getElementsByAttributeValue(attributeKey, attributeValue);
}
return resultElements;
}

/**
* 根据属性key值是否以特定字符串开头获取元素集合
* @param document
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeNameStartWithValue(Document document,String keyValue)
{
Elements elements=null;
if(document!=null&&keyValue!=null&&!"".equals(keyValue.trim()))
{
elements=document.getElementsByAttributeStarting(keyValue);
}
return elements;
}
/**
* 根据属性key值是否以特定字符串开头获取元素集合
* @param element
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeNameStartWithValue(Element element,String keyValue)
{
Elements elements=null;
if(element!=null&&keyValue!=null&&!"".equals(keyValue.trim()))
{
elements=element.getElementsByAttributeStarting(keyValue);
}
return elements;
}
/**
* 根据属性value值是否被包含在某个元素的某个属性中获取元素集合
* @param document
* @param containValue
* @return
*/
public static Elements getElementsByAttributeValueContaining(Document document,String attributeKey,String containValue)
{
Elements elements=null;
if(document!=null&&containValue!=null&&!"".equals(containValue))
{
elements=document.getElementsByAttributeValueContaining(attributeKey, containValue);
}
return elements;
}

/**
*  根据属性value值是否被包含在某个元素的某个属性中获取元素集合
* @param element
* @param attributeKey
* @param containValue
* @return
*/
public static Elements getElementsByAttributeValueContaining(Element element,String attributeKey,String containValue)
{
Elements elements=null;
if(element!=null&&containValue!=null&&!"".equals(containValue))
{
elements=element.getElementsByAttributeValueContaining(attributeKey, containValue);
}
return elements;
}
/**
* 根据属性的value值是否以某个字符串结尾获取元素集合
* @param document
* @param attributeKey
* @param valueSuffix
* @return
*/
public static Elements getElementsByAttributeValueEnding(Document document,String attributeKey,String valueSuffix)
{
Elements elements=null;
if(document!=null&&attributeKey!=null&&!"".equals(attributeKey)&&valueSuffix!=null&&!"".equals(valueSuffix))
{
elements=document.getElementsByAttributeValueEnding(attributeKey, valueSuffix);
}
return elements;
}

/**
* 根据属性的value值是否以某个字符串结尾获取元素集合
* @param element
* @param attributeKey
* @param valueSuffix
* @return
*/
public static Elements getElementsByAttributeValueEnding(Element element,String attributeKey,String valueSuffix)
{
Elements elements=null;
if(element!=null&&attributeKey!=null&&!"".equals(attributeKey)&&valueSuffix!=null&&!"".equals(valueSuffix))
{
elements=element.getElementsByAttributeValueEnding(attributeKey, valueSuffix);
}
return elements;
}
/**
* 根据属性值value的正则表达式获取元素集合
* @param document
* @param attributeKey
* @param pattern
* @return
*/
public static Elements getElementsByAttributeValueMatching(Document document,String attributeKey,Pattern pattern)
{
Elements elements=null;
if(document!=null&&attributeKey!=null&&!"".equals(attributeKey)&&pattern!=null)
{
elements=document.getElementsByAttributeValueMatching(attributeKey, pattern);
}
return elements;
}

/**
* 根据属性值value的正则表达式获取元素集合
* @param element
* @param attributeKey
* @param pattern
* @return
*/
public static Elements getElementsByAttributeValueMatching(Element element,String attributeKey,Pattern pattern)
{
Elements elements=null;
if(element!=null&&attributeKey!=null&&!"".equals(attributeKey)&&pattern!=null)
{
elements=element.getElementsByAttributeValueMatching(attributeKey, pattern);
}
return elements;
}

/**
* 根据属性值的value的正则表达式获取元素集合
* @param document
* @param attributeKey
* @param regualRegx
* @return
*/
public static Elements getElementsByAttributeValueMatching(Document document,String attributeKey,String regualRegx)
{
Elements elements=null;
if(document!=null&&attributeKey!=null&&!"".equals(attributeKey)&&regualRegx!=null&&!"".equals(regualRegx))
{
elements=document.getElementsByAttributeValueMatching(attributeKey, regualRegx);
}
return elements;
}

/**
* 根据属性值的value的正则表达式获取元素集合
* @param element
* @param attributeKey
* @param regualRegx
* @return
*/
public static Elements getElementsByAttributeValueMatching(Element element,String attributeKey,String regualRegx)
{
Elements elements=null;
if(element!=null&&attributeKey!=null&&!"".equals(attributeKey)&&regualRegx!=null&&!"".equals(regualRegx))
{
elements=element.getElementsByAttributeValueMatching(attributeKey, regualRegx);
}
return elements;
}
/**
* 返回属性键attributeKey不等于值attributeValue的元素集合
* @param document
* @param attributeKey
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeValueNot(Document document,String attributeKey,String attributeValue)
{
Elements elements=null;
if(document!=null&&attributeKey!=null&&!"".equals(attributeKey)&&attributeValue!=null&&!"".equals(attributeValue))
{
elements=document.getElementsByAttributeValueNot(attributeKey,attributeValue);
}
return elements;
}

/**
* 返回属性键attributeKey不等于值attributeValue的元素集合
* @param element
* @param attributeKey
* @param attributeValue
* @return
*/
public static Elements getElementsByAttributeValueNot(Element element,String attributeKey,String attributeValue)
{
Elements elements=null;
if(element!=null&&attributeKey!=null&&!"".equals(attributeKey)&&attributeValue!=null&&!"".equals(attributeValue))
{
elements=element.getElementsByAttributeValueNot(attributeKey,attributeValue);
}
return elements;
}
/**
* 根据选择器匹配的字符串返回
* Elements(元素集合)
* @param document
* @param selectStr 选择器(类似于JQuery)
* @return
*/
public static Elements getMoreElementsBySelectStr(Document document,String selectStr)
{
if(document==null||selectStr==null||"".equals(selectStr.trim()))
{
return null;
}
else
{
Elements elements=document.select(selectStr);
if(elements!=null&&elements.size()>0)
{
return elements;
}
else
{
return null;
}
}
}

/**
*根据选择器匹配的字符串返回
* Elements(元素集合)
* @param element
* @param selectStr
* @return
*/
public static Elements getMoreElementsBySelectStr(Element element,String selectStr)
{
if(element==null||selectStr==null||"".equals(selectStr.trim()))
{
return null;
}
else
{
Elements elements=element.select(selectStr);
if(elements!=null&&elements.size()>0)
{
return elements;
}
else
{
return null;
}
}
}
/**
  * 根据选择器匹配的字符串返回
* Element(单个元素)
* @param document
* @param selectStr 选择器(类似于JQuery)
* @return
*/
public static Element getSingleElementBySelectStr(Document document,String selectStr)
{
Elements elements=getMoreElementsBySelectStr(document,selectStr);
if(elements!=null&&elements.size()>0){
return elements.get(0);
}
else
{
return null;
}
}

/**
  * 根据选择器匹配的字符串返回
* Element(单个元素)
* @param element
* @param selectStr
* @return
*/
public static Element getSingleElementBySelectStr(Element element,String selectStr)
{
Elements elements=getMoreElementsBySelectStr(element,selectStr);
if(elements!=null&&elements.size()>0){
return elements.get(0);
}
else
{
return null;
}
}
/**
* 根据选择器匹配的字符串返回单个元素的Html字符串
* @param document
* @param selectStr 选择器(类似于JQuery)
* @return
*/
public static String getSingleElementHtmlBySelectStr(Document document,String selectStr)
{
Element element=getSingleElementBySelectStr(document,selectStr);
if(element!=null)
{
return element.html();
}
else
{
return null;
}
}

/**
* 根据选择器匹配的字符串返回单个元素的Html字符串
* @param element
* @param selectStr
* @return
*/
public static String getSingleElementHtmlBySelectStr(Element element,String selectStr)
{
Element ele=getSingleElementBySelectStr(element,selectStr);
if(ele!=null)
{
return ele.html();
}
else
{
return null;
}
}

/**
* 根据元素属性名key获取元素属性名value
* @param element
* @param attributeName
* @return
*/
public static String getAttributeValue(Element element,String attributeName)
{
String attributeValue=null;
if(element!=null&&attributeName!=null&&!"".equals(attributeName))
{
attributeValue=element.attr(attributeName);
}
return attributeValue;
}

/**
* 从elements集合中获取element并解析成HTML字符串
* @param elements
* @return
*/
public static String getSingElementHtml(Elements elements){
Element ele=null;
String htmlStr=null;
if(elements!=null&&elements.size()>0)
{
ele=elements.get(0);
htmlStr=ele.html();
}
return htmlStr;
}
/**
* 从elements集合中获取element并解析成Text字符串
* @param elements
* @return
*/
public static String getSingElementText(Elements elements){
Element ele=null;
String htmlStr=null;
if(elements!=null&&elements.size()>0)
{
ele=elements.get(0);
htmlStr=ele.text();
}
return htmlStr;
}
public static void main(String[] args)
{
File file=new File("F:/example.htm");
try {
Document document=Jsoup.parse(file,"GB2312");
Pattern pattern=Pattern.compile("");
// document.getElementsByAttributeValueMatching("", pattern);
Element element=getElementById(document,"personal-uplayer");


} catch (IOException e) {
e.printStackTrace();
}
}
}
分享到:
评论

相关推荐

    java开源包1

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包11

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包2

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包3

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包6

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包5

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包10

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包4

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包8

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包7

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包9

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包101

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    Java资源包01

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    JAVA上百实例源码以及开源项目源代码

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

    JAVA上百实例源码以及开源项目

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

Global site tag (gtag.js) - Google Analytics