Link to home
Start Free TrialLog in
Avatar of akzah
akzah

asked on

Parsing a String (BufferReader)

Hi

I am using the following code and a bit more to download a html file from a website and it is then turned into a string.

URLConnection connection = javacodingURL.openConnection();
BufferedReader br = new BufferedReader(newInputStreamReader(connection.getInputStream()));
String line = "";
while ((line = br.readLine()) != null)
System.out.println(line);
br.close();

My code works, and I get a ouput of the html file. Though what I want is specfic text but I don't want to use the HTMLEditor from java as it causes too many problems. The html file has some code at the start which I want to ignore and it then follows a pattern:

It starts with <p align="center">............(code I need)......<hr color="#87A9DC">. Then again it starts with  <p align="center">............(code I need)......<hr color="#87A9DC"> (goes on in that format till then end)

My question is how would I get the all the html code between the p align code and hr color and place the data individualy into a arraylist?

Thanks for any help
Akbar
Avatar of zzynx
zzynx
Flag of Belgium image

Method:
1) Search your string for "<p align="center">"
2) From that place search for "<hr color="
3) Get that in between.

The function String.indexOf() will be useful:

int indexOf(String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akzah
akzah

ASKER

that method sounds good, though how would I get that to work on the BufferedReader br. How would I turn that into a string?
StringBuffer buff = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null)
     buff.append(line);

String allTheText = buff.toString();
Thanks for accepting akzah
:)
That keeps us answering your future questions too.