用java解析html標籤

來源:互聯網
上載者:User

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hothouseobjects.tags.Inspector;
import com.hothouseobjects.tags.Tag;
import com.hothouseobjects.tags.TagTiller;
import com.hothouseobjects.tags.Text;

public class ParseHtml {

public static void main(String[] args) {
        try {
            File file = new File("d://ttt.htm");
            int len = (int)file.length();
            byte[] b;
            b = new byte[len];
            FileInputStream fis = new FileInputStream(file);
            fis.read(b);
            fis.close();
            Reader read;
            read = new StringReader(new String(b));

            TagTiller tagtiller = new TagTiller(read);
            tagtiller.runTiller();
            Tag thePage = tagtiller.getTilledTags();
// search <a href=......
            List theHref = Inspector.collectByType(thePage,"a");
            int i = theHref.size();
            while (i>0) {
                //System.out.println(((Tag)theHref.get(i-1)).toHTML());
                System.out.println(((Tag)theHref.get(i-1)).getAttributeValue("href"));
              //  System.out.println(((Tag)theHref.get(i-1)).getAttributeValue("target"));
               // System.out.println(((Tag)theHref.get(i-1)).getItem(0).toHTML());
                i -=1;
            }
// search <img src=.....
            List theImg = Inspector.collectByType(thePage,"img");
            int j = 0;
            while (j < theImg.size()) {
                System.out.println(((Tag)theImg.get(j)).toHTML());
                System.out.println(((Tag)theImg.get(j)).getAttributeValue("src"));
                System.out.println(((Tag)theImg.get(j)).getAttributeValue("alt"));
                j +=1;
            }
// out put file of html
            Tag big = new Tag ("H1");
            Tag red = new Tag ("FONT");
            Tag underlined = new Tag ("U");

            red.setAttribute ("color", "#CCOOOO");
            big.addItem (red);
            red.addItem (underlined);
            underlined.addItem (new Text ("Sorry, no addresses found."));
            Tag title = Inspector.locateByType(thePage,"title");
            title.addItem(big);

            StringWriter writer = new StringWriter();
            thePage.toHTML(writer);
            File file1 = new File("test000.html");

            FileOutputStream fos = new FileOutputStream(file1);

            fos.write(writer.toString().getBytes());
            fos.close();
// search of <table
            List theTd = Inspector.collectByType(thePage,"td");
            int k = 0;
            while (k < theTd.size()) {
                System.out.println(((Tag)theTd.get(k)).toHTML());
                k +=1;
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.