Extract content between two tags in HTML document

Hi.

I have an HTML document, contained in a String (using this code: http://www.javaalmanac.com/egs/javax.swing.text.html/GetText.html).

Now, I would like to extract the String between the <title> tag, and the </title> tag.

(The title tags will *always* be in this document).

Any example code for this? Should I just use StringTokenizer, setting the delimiter to "<title>", then in the second token, run the StringTokenizer class again, to extract the first token, when the delimiter is "</title>" ??

That's the best idea that I can come up with.

Thanks in advance,
>> IM
LVL 25
InteractiveMindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lhankinsCommented:
Here's a working example :

      String someHtml =
              "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
              "<html>\n" +
              "<head>\n" +
              "<title>this is my title</title>\n" +
              "\n" +
              "<style type=\"text/css\">\n" +
              "</style>\n" +
              "\n" +
              "<script type=\"text/javascript\">\n" +
              "</script>\n" +
              "\n" +
              "\n" +
              "</head>\n" +
              "\n" +
              "<body>\n" +
              "   <div>bleh</div>\n" +
              "</body>\n" +
              "\n" +
              "</html>".toLowerCase();

      String titleStartTag = "<title>";
      String titleEndTag = "</title>";

      int start = someHtml.indexOf(titleStartTag);
      int end = someHtml.indexOf(titleEndTag);

      if (start != -1 && end !=-1)
      {
         String titleText = someHtml.substring(start + titleStartTag.length(), end);

         System.out.println("title inner text is [" + titleText + "]");
      }



----------------------

Output when run is :

    title inner text is [this is my title]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhankinsCommented:
BTW - I did a .toLowerCase() on the whole HTML string because title could appear as <title> or <TITLE>.      This will also make the inner text lower case.  To get around this, you could find the <title> start and end via the lowercase string, then fall back to the original HTML string when doing the .substring.

0
InteractiveMindAuthor Commented:
Fantastic! Thank you very much.  :)
>> IM
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.