This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

retrieving string from a webpage

 
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
 
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.
 
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:

 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!