CS 1705 Library

cs1705.web
Interface HtmlElement

All Known Subinterfaces:
HtmlHeadingElement

public interface HtmlElement

This interface represents an instance of some HTML element (or "tag"), usually obtained by reading from a web page using a WebBot. As an example, consider this HTML fragment:

  <img src="logo.jpg" width="100" height="50"/>
  

When parsed by a WebBot, this HTML would be converted to a single HtmlElement object. That object's "type" would be "img". It would have three attributes: "src", "width", and "height". Each of these attributes could be retrieved using getAttributeValue(String). In this case, there would be no text value associated with the tag, since it has no children (in a DOM sense). As another example, consider this second HTML fragment:

  <a href="http://www.vt.edu/">Virginia Tech</a>
  

When parsed by a WebBot, this HTML would be converted to a single HtmlElement object as well. That object's type would be "a". It would have one attribute: "href". If you called its getText() method, you would get "Virginia Tech".

Version:
2008.03.29
Author:
Stephen Edwards

Method Summary
 Iterable<String> getAttributes()
          Get an Iterable that will allow you to cycle through the complete set of attributes on this element.
 String getAttributeValue(String attributeName)
          Look up an attribute's value on this element.
 String getText()
          Get the text contained by this element.
 String getType()
          Get the "type" of this element--that is, the HTML name of the tag, such as "a", "h1", "img", etc.
 boolean hasAttribute(String attributeName)
          Check to see if this element has a specific attribute.
 String toString()
          Generate a complete HTML rendering of this element, all its attributes, and any text that it contains.
 

Method Detail

getType

String getType()
Get the "type" of this element--that is, the HTML name of the tag, such as "a", "h1", "img", etc. Note that the element type is case-sensitive in XHTML or XML documents.

Returns:
The type of this tag

getText

String getText()
Get the text contained by this element.

Returns:
The text surrounded by this element in the original document, or null if none

hasAttribute

boolean hasAttribute(String attributeName)
Check to see if this element has a specific attribute.

Parameters:
attributeName - The attribute to check for
Returns:
True if this element has the specified attribute

getAttributeValue

String getAttributeValue(String attributeName)
Look up an attribute's value on this element.

Parameters:
attributeName - The attribute to look up
Returns:
The attribute's value for this element, or null if there is none

getAttributes

Iterable<String> getAttributes()
Get an Iterable that will allow you to cycle through the complete set of attributes on this element.

Returns:
An iterator suitable for use in a for-each loop

toString

String toString()
Generate a complete HTML rendering of this element, all its attributes, and any text that it contains.

Overrides:
toString in class Object
Returns:
A printable version of this element

Last updated: Wed, Apr 1, 2009 • 12:29 AM EDT

Copyright © 2009 Virginia Tech.