Solved

Parsing a String (BufferReader)

Posted on 2004-04-21
5
421 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:akzah
  • 4
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10877631
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.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 10877747
String yourString = ... // gor from your file

List arrayList = new ArrayList();

int i1 = 0;
int i2 = 0;
int start = 0;
i1 = yourString.indexOf("<p align", 0);
start = yourString.indexOf(">", i1);
i2 = yourString.indexOf("<hr color", i1);

Visually:

xwvqsdf <p align="center">My text fgqsfgsd dsqg sdgsdf sdfgsd sdfsg End of my text<hr color=#xxxxxx>bla bla bla
             |                         |                                                                                 |
            i1                         start                                                                            i2

arrayList.add( yourString.substring(start, i2) );  // adding "My text fgqsfgsd dsqg sdgsdf sdfgsd sdfsg End of my text"

You can restart for another loop when you perform:

yourString = yourString.substring(i2);
0
 

Author Comment

by:akzah
ID: 10877766
that method sounds good, though how would I get that to work on the BufferedReader br. How would I turn that into a string?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10877835
StringBuffer buff = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null)
     buff.append(line);

String allTheText = buff.toString();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10878603
Thanks for accepting akzah
:)
That keeps us answering your future questions too.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where to store the queries for modification of table 4 63
windows explorer path to command prompt 5 45
login jsp example 24 51
Chrome and Firefox Java 5 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question