<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    MDA/MDD/TDD/DDD/DDDDDDD
    posts - 536, comments - 111, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    html的解析以及nekohtml的使用

    Posted on 2008-02-21 18:29 leekiang 閱讀(2902) 評論(0)  編輯  收藏 所屬分類: 文件處理
    ??? import ?org.cyberneko.html.parsers.DOMFragmentParser;
    ???? import org.apache.html.dom.HTMLDocumentImpl;
    ????
    import ?org.w3c.dom.DocumentFragment;
    ????
    import ?org.w3c.dom.Node;
    ????
    import ?org.w3c.dom.NodeList;
    ????
    import ?org.xml.sax.InputSource;
    ????
    import ?org.xml.sax.SAXException;

    ????
    /**
    ?????*?從html中抽取純文本
    ?????*?
    ?????*?
    @param ?content
    ?????*?
    @return
    ?????*?
    @throws ?UnsupportedEncodingException
    ?????
    */
    ????
    public ?String?extractTextFromHTML(String?content)
    ????????????
    throws ?UnsupportedEncodingException?{
    ????????DOMFragmentParser?parser?
    = ? new ?DOMFragmentParser();
    ????????DocumentFragment?node?
    = ? new ?HTMLDocumentImpl().createDocumentFragment();
    ????????InputStream?is?
    = ? new ?ByteArrayInputStream(content.getBytes());
    ????????
    try ?{
    ????????????parser.parse(
    new ?InputSource(is),?node);
    ????????}?
    catch ?(IOException?e)?{
    ????????????e.printStackTrace();
    ????????}?
    catch ?(SAXException?se)?{
    ????????????se.printStackTrace();
    ????????}

    ????????StringBuffer?newContent?
    = ? new ?StringBuffer();
    ????????
    this .getText(newContent,?node);

    ????????String?str?
    = ?( new ?String(
    ????????????????newContent.toString().getBytes(
    " Windows-1252 " ),? " GBK " ));
    ????????
    return ?str;
    ????}

    ????
    private ? void ?getText(StringBuffer?sb,?Node?node)?{
    ????????
    if ?(node.getNodeType()? == ?Node.TEXT_NODE)?{
    ????????????sb.append(node.getNodeValue());
    ????????}
    ????????NodeList?children?
    = ?node.getChildNodes();
    ????????
    if ?(children? != ? null )?{
    ????????????
    int ?len? = ?children.getLength();
    ????????????
    for ?( int ?i? = ? 0 ;?i? < ?len;?i ++ )?{
    ????????????????getText(sb,?children.item(i));
    ????????????}
    ????????}
    ????}

    1,nekohtml1.9.6.1版本用到了jdk5的Arrays.hashCode等方法,為兼容jdk1.4,
    ? 故采用nekohtml1.9.6版本
    2,需要xerces.jar支持
    3,
    ?? http://hi.baidu.com/walkandsing/blog/item/f5743634c6ba2e3a5bb5f5e5.html
    ?? http://blog.csdn.net/zhou2002/archive/2008/01/19/2053911.aspx
    ?? http://playfish.javaeye.com/blog/150184

    4,
    python解析html
    http://lenciel.cn/docs/python-parser-of-xml/
    http://hi.baidu.com/javalang/blog/item/84bac4bf731fb80f18d81fe1.html
    ruby用hpricot

    主站蜘蛛池模板: 九九综合VA免费看| 暖暖日本免费中文字幕| 亚洲欧洲日产专区| 天天拍拍天天爽免费视频| 特黄特色的大片观看免费视频| 久久精品国产精品亚洲精品 | 鲁丝片一区二区三区免费| 亚洲mv国产精品mv日本mv| 免费国内精品久久久久影院| 久久久久久久久久国产精品免费 | 亚洲图片校园春色| 亚洲国产aⅴ综合网| 免费专区丝袜脚调教视频| 四虎影视在线看免费观看| 亚洲成人黄色在线| 国产亚洲精品影视在线产品| 国产啪精品视频网免费| 中文字幕在线免费看| 国产成人亚洲综合一区| 日韩va亚洲va欧洲va国产| 妞干网免费视频观看| 日本免费人成网ww555在线| 国产精品亚洲精品日韩动图| 亚洲日本va午夜中文字幕一区| 国产大片91精品免费观看男同| 曰批视频免费40分钟试看天天| 一个人免费播放在线视频看片 | 一个人免费日韩不卡视频| 免费人成视频在线观看免费| 亚洲国产精品成人久久久 | 免费国产a理论片| 亚洲国产电影在线观看| 国产亚洲精品拍拍拍拍拍| 性色av免费观看| 成人免费视频69| 国产日韩AV免费无码一区二区| 在线精品自拍亚洲第一区| 亚洲午夜电影一区二区三区| 久久亚洲精品成人综合| 亚洲国产精品13p| 四虎永久免费地址在线观看|