Click here to Skip to main content
11,644,731 members (69,173 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java HTML Eclipse url netbeans , +
Hello all,

today I had some business with html parsing. The requested result was: using the class get all html content from and set up a file which can be used to view the website offline. The greatest problem turned out to be "fetching" the html elements attributes like src from an <img></img> tag, href from an
tag etc. So far I have got to the src attribute by using regular expressions and BufferedReader/Writer classes. A code sample:
URL google = new URL("");
        BufferedReader in = new BufferedReader(new InputStreamReader(google
        BufferedWriter wr;
        String s = null;
        Pattern p;
        p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)",Pattern.CASE_INSENSITIVE);
        Matcher m;
        try {
            wr = new BufferedWriter(new FileWriter("D:/HTMLFile.txt"));
            while ((s = in.readLine()) != null) {
                    m = p.matcher(s);
                while(m.find()) {
        } catch (IOException ex) {
            Logger.getLogger(JavaNetworking.class.getName()).log(Level.SEVERE, null, ex);
For this particular URL the output is: "/textinputassistant/tia.png"

What I wanted to ask, is can someone give a better example on how to do this? I read on various forums that regex + java is a hidious monster, sort of speak. I have an algorithm in mind that could lighten stuff up for an experienced programmer, unlike me Smile | :) it is.
- read all html from the URL
- copy to a string variable
- search in string for "<img"
- when "<img"> - copy to new string variable
- search for "src" or "href" attribute
- extract the attributes value (System.out.println("..") will do just fine for now)
I see this is an idiot-proof problem since I think that this could work out just fine like this, but still I think it's better to ask for an oppinion from a community made of waaay bigger professionals Smile | :)
Posted 22-Nov-12 11:39am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please read here: RegEx Tutorial[^] @

And please do some research here, we had such things in the past.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 680
1 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 418
4 CPallini 336
0 OriginalGriff 925
1 DamithSL 636
2 Afzaal Ahmad Zeeshan 622
3 Sergey Alexandrovich Kryukov 548
4 CPallini 521

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100