Further to my earlier question here: Extending a basic web crawler to filter status codes and HTML , I'm trying to extract information from HTML tags, in this case "title", with the following method:
public static void parsePage() throws IOException, BadLocationException
{
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
Reader HTMLReader = new InputStreamReader(testURL.openConnection()
.getInputStream());
kit.read(HTMLReader, doc, 0);
// Create an iterator for all HTML tags.
ElementIterator it = new ElementIterator(doc);
Element elem;
while ((elem = it.next()) != null)
{
if (elem.getName().equals("title"))
{
System.out.println("found title tag");
}
}
}
This is working as far as telling me it's found the tags. What I'm struggling with is how to extract the information contained after/within them.
I found this question on the site: Help with Java Swing HTML parsing , however it states it will only work with well-formed HTML. I was hoping there is another way.
Any pointers appreciated.