Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving string from a webpage

 
bhagavatula indu sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling getText immediately after setting the page url. I guess the content isnt entirely loaded before you call getText.
 
Eitan Levi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic