Solved

Parsing a String (BufferReader)

Posted on 2004-04-21
5
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 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