It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes retrieving string from a webpage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "retrieving string from a webpage" Watch "retrieving string from a webpage" New topic
Author

retrieving string from a webpage

bhagavatula indu sekhar
Ranch Hand

Joined: Sep 17, 2006
Posts: 34
hi i am required to do some extractions from a webpage and do some pattern matchings.So i made use of JEditorPane class

the method getText() of jep(an object of the class JEditorPane) is not working properly its displaying the proper contents only once

here is the code which i have written

import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.net.*;
import java.awt.*;

public class jeditor extends JFrame
{
Container c;
URL url;
JEditorPane jep;

public static void main(String a[])
{
jeditor obj=new jeditor();
}

jeditor()
{
c=getContentPane();
c.setLayout(new FlowLayout());
try
{
url=new URL("file:///C:/1.html");
}
catch(MalformedURLException murle)
{
System.out.println("\nURL not formed properly");
System.exit(-1);
}

catch(Exception e)
{
System.out.println("\nError1");
System.exit(-1);
}

try
{
jep=new JEditorPane();
jep.setContentType("text/html");
jep.setPage(url);
c.add(jep);
}
catch(Exception e)
{
System.out.println("\nError2!");
System.exit(-1);
}

show();
setSize(500,500);

String s=jep.getText();
System.out.println(s);

JOptionPane.showMessageDialog(null,"Page Displayed");
jep.setText("");
try
{
url=new URL("file:///C:/2.html");
jep.setPage(url);
}
catch(Exception e)
{
System.out.println("\nError3!");
System.exit(-1);
}

s=jep.getText();

System.out.println(s);


}
}

please give me a solution to this problem

thanq
Anay Nayak
Greenhorn

Joined: Aug 15, 2006
Posts: 26
You are calling getText immediately after setting the page url. I guess the content isnt entirely loaded before you call getText.
Eitan Levi
Greenhorn

Joined: Jun 14, 2006
Posts: 14
If all you need to do is read content from a URL, and you don't actually need any GUI, then why not just use the URL's openStream() method? Then you could just read from the URL into a StringBuilder or CharBuffer, or use a BufferedReader and read line by line...

Something like this:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: retrieving string from a webpage
 
Similar Threads
JEditorPane problem displaying non-English text in 1.4 (Linux)
Help me to solve these problems in JEditorPane
How to make a JavaBrowser
display web page
Loading Webpage in JFrame