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

Discussion in 'Java' started by Jozza, Mar 1, 2010.

  1. Jozza

    Jozza Guest

    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.
     
    Jozza, Mar 1, 2010
    #1
    1. Advertising

  2. Jozza

    Jozza Guest


    > 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_DECLARATION, "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
     
    Jozza, Mar 4, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    1
    Views:
    6,470
    Darren Davison
    Aug 5, 2003
  2. mynamehere
    Replies:
    0
    Views:
    485
    mynamehere
    Dec 14, 2003
  3. Beat Zahnd
    Replies:
    3
    Views:
    7,465
    Beat Zahnd
    Apr 14, 2005
  4. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    1,026
    Johannes Koch
    Aug 7, 2003
  5. Alan
    Replies:
    6
    Views:
    1,704
Loading...

Share This Page