Java Reference
In-Depth Information
e.printStackTrace();
}
}
}
This recipe begins by opening a connection to the URL it will download from.
URL url = new URL("http://www.httprecipes.com/1/9/includes.php");
InputStream is = url.openStream();
A ParseHTML object will be used to parse the HTML. The ParseHTML class was
discussed in Chapter 6.
ParseHTML parse = new ParseHTML(is);
StringBuilder buffer = new StringBuilder();
As the data is read from the HTML page, each tag is processed. If the tag found
is a <script> tag, then we will look to see if it is a JavaScript include. Additionally, a
StringBuilder is setup to hold the compound document.
int ch;
while ((ch = parse.read()) != -1)
{
if (ch == 0)
{
If this tag is a <script> tag, and it has a src attribute, then the tag will be processed
as a JavaScript include.
HTMLTag tag = parse.getTag();
if (tag.getName().equalsIgnoreCase("script") &&
tag.getAttributeValue("src")!=null)
{
The included page is loaded and appended to the main document. Included JavaScript
pages do not have a beginning <script> and ending </script> tags of their own, so
these tags are added.
String src = tag.getAttributeValue("src");
URL u = new URL(url,src);
String include = downloadPage(u);
buffer.append("<script>");
buffer.append(include);
buffer.append("</script>");
}
else
{
If this is a regular HTML tag, then append it to the StringBuilder .
buffer.append(tag.toString());
Search WWH ::




Custom Search