2012-03-05 60 views
1

我有一个字符串,我想从中列出其中存在的所有HTML标记。有没有任何图书馆可以做这项工作?从字符串中列出HTML标记

任何信息对我都很有帮助。

+0

看看这里,我想你会找到你想要的一切 - > http://java-source.net/open-source/html-parsers – tartak 2012-03-05 11:59:43

+0

你也许可以使用Jtidy,查看http:// jtidy.sourceforge.net/howto.html – Sap 2012-03-05 12:00:34

+0

http://htmlcleaner.sourceforge.net – edze 2012-03-05 12:02:36

回答

1

您可以使用下面的代码从字符串中仅提取HTML标记。

package com.overflow.stack; 

    /** 
    * 
    * @author sarath_sivan 
    */ 

    public class ExtractHtmlTags { 

     public static void getHtmlTags(String html) { 
      int beginIndex = 0; 
      while(beginIndex!=-1) { 
       beginIndex = html.indexOf("<", 0); 
       int endIndex = html.indexOf(">", beginIndex+1); 
       String htmlTag = ""; 
       try { 
        if(beginIndex!=-1) { 
         htmlTag = html.substring(beginIndex, endIndex+1); 
        } 
       } catch(Exception e) { 
        e.printStackTrace(); 
       } 
       System.out.println(htmlTag); 
       html = html.substring(endIndex+1, html.length()); 
      } 
     } 

     public static void main(String[] args) { 
      String html = "<html><body><h2>List HTML tags from a String</h2>hello<br /></body></html>"; 
      ExtractHtmlTags.getHtmlTags(html); 
     } 

    } 

但是,我不明白你想要用提取的HTML标签做什么。祝你好运!

0
page = Nokogiri::HTML(open('http://yoursite.com')) 
page.css("*").map{|x| x.name}.flatten.uniq