Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > read html file into org.w3c.dom.html.htmldocument

Reply
Thread Tools

read html file into org.w3c.dom.html.htmldocument

 
 
Jozza
Guest
Posts: n/a
 
      03-01-2010
Hi,

how do i read a local html file into a org.w3c.dom.html.htmldocument ?
I want to dynamically change the values of the elements in html.

TIA J.


 
Reply With Quote
 
 
 
 
Jozza
Guest
Posts: n/a
 
      03-04-2010

> Hi,
>
> how do i read a local html file into a org.w3c.dom.html.htmldocument ?
> I want to dynamically change the values of the elements in html.
>
> TIA J.
>


Well, there it is, i wrote it myself, it wasnt easy since i'm not a complete
java nut:

import java.io.*;
import org.cyberneko.html.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import org.w3c.dom.html.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public String LoadHTML(String filename)
{
Document doc;
FileInputStream inStream;
String s = new String();
String urlIS = new String(filename);
try
{
inStream = new FileInputStream(urlIS);
}
catch (Exception e)
{
return "";
}
DOMParser parser = new DOMParser();
try
{
parser.parse(new InputSource(inStream));
}
catch (Exception e)
{
return "";
}
doc = parser.getDocument();
HTMLInputElement hi =
(HTMLInputElement)doc.getElementById("search");
if (hi!=null)
{
s = hi.getValue().toString();
hi.setValue("seeearch");
s = hi.getValue().toString();
}
StringWriter sw = new StringWriter();
Transformer t = null;
try
{
t = TransformerFactory.newInstance().newTransformer();
}
catch (Exception e)
{
return "";
}
t.setOutputProperty(OutputKeys.METHOD, "html");
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATIO N, "yes");
try
{
t.transform(new DOMSource(doc), new StreamResult(sw));
}
catch (Exception e)
{
return "";
}
s = sw.toString();
return s;
}

Ufcourse, there seem to be one million other ways to do it


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
using dreamweaver : how can I insert HTML file into another html file? johnsonholding@yahoo.com HTML 3 01-10-2006 08:06 PM
How to read the BODY of HTML file from another HTML file using javascript? Dhanasankar S via DotNetMonster.com ASP .Net 1 02-28-2005 05:51 PM
How to read the BODY of HTML file from another HTML file using javascript? Dhanasankar S via DotNetMonster.com ASP .Net 0 02-26-2005 10:58 AM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM



Advertisments