Menu

reading from a file instead of a page

Help
2005-02-05
2013-04-27
  • javacodeforger

    javacodeforger - 2005-02-05

    Hi,

    how do I parse a file on my pc

          File file = new File(filepath);

          Parser parser = new Parser (file.getAbsolutePath());
          NodeIterator ni = parser.elements();
          Node htmlNode = ni.nextNode();

    I tried this
    but it threw and error

    org.htmlparser.util.ParserException: D:\program\java\src\resources\wb1931_a.html (The system cannot find the file specified);

    the file is in that directory

    pls help

     
    • Derrick Oswald

      Derrick Oswald - 2005-02-05

      There is code in Parser.java to do this.

                      final String prefix = "file://localhost";
                      File file = new File (string);
                      String resource = file.getCanonicalPath ();
                      StringBuffer buffer = new StringBuffer (prefix.length () + resource.length ());
                      buffer.append (prefix);
                      if (!resource.startsWith ("/"))
                          buffer.append ("/");
                      buffer.append (resource);
                      url = new URL (fixSpaces (buffer.toString ()));

      then you can use url.toExternalForm().

       
    • jeffChan

      jeffChan - 2005-03-01

      I have tried this code, but I cannot get any content after parser.extractAllNodesThatMatch (filter);
      although I sucessfully complied the code

       
      • Derrick Oswald

        Derrick Oswald - 2005-03-01

        Try a less restrictive filter, or if the problem is trying to re-get nodes after calling extractAllNodesThatMatch() then use parser.reset().

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.