Unable to catch lines from html file
This is my html file
...
...
...
<option value="PMC">PMC</option>
</select>
Email:
<Input type=text name=email value="">
?File: <Input type=file name=file size=30>
<textarea name="bdata" cols="70" rows="20">title|1998|28|209|name|b2|9483698
title|1998|14|93|name|b3|12766437
...
...
...
This is my source code
while((str = in.readLine()) !=null)
{
if(str.substring(str.indexOf("<")+1).equals("textarea name=\"bdata\" cols=\"70\" rows=\"20\">"))
{
flag=true;
continue;
}
}
From my codeif(str.substring(str.indexOf("<")+1).equals("textarea name=\"bdata\" cols=\"70\" rows=\"20\">"))
, I am trying to catch the contents of html file between <textarea name="bdata" cols="70" rows="20">
and </textarea>
. But I am not able to do so. Could anyone please tell me what is wrong in my code.
[1644 byte] By [
Simmya] at [2007-11-27 8:15:31]

The equals method will not work because your line goes on with "title|1998.. "
maybe the following will do what you want :
while((str = in.readLine()) != null)
{
if(str.substring(str.indexOf("<")+1).indexOf("textarea name=\"bdata\" cols=\"70\" rows=\"20\">")==0)
{
flag=true;
continue;
}
}
From your code I am able to catch all the lines except the first line, that is
<textarea name="bdata" cols="70" rows="20">title|1998|28|209|name|b2|9483698
Could you please tell me what to modify more.
Simmya at 2007-7-12 20:00:19 >

If I give it as
if((str.substring(str.indexOf("<")+1).indexOf("textarea name=\"bdata\" cols=\"70\" rows=\"20\">")+1)==0)
the code is catching only the first line, that is
title|1998|28|209|name|b2|9483698
Simmya at 2007-7-12 20:00:19 >

I am sorry, i thought that you had only posted the part of the code, that was searching for the beginning of your textarea .
Because otherwise the flag = true line does not make sense !
String sContent = "" ;
boolean flag = false ;
String sPattern = "<textarea name=\"bdata\" cols=\"70\" rows=\"20\">" ;
String sEnd = "</textarea" ;
while((str = in.readLine()) != null) {
if (str.indexOf(sPattern)>=0) {
// we found the beginning ;
flag = true ;
sContent = str.substring(str.indexOf(sPattern)+sPattern.length()) ;
} else if (str.indexOf(sEnd)>=0) {
flag = false ;
} else if (flag) {
sContent = sContent + str ;
}
}
It is not tested but it should be near to what you are asking for. The code does not work if the textarea start and end tag are within the same line.
If you're not using some sort of html parser, then I suggest reading your html file in one String and then use some (simple) regex to macth for a pattern.
String htmlContents = "..."+
"..."+
"..."+
"<option value=\"PMC\">PMC</option>"+
"</select>"+
"
"+
"Email:"+
"<Input type=text name=email value=\"\">"+
"File: <Input type=file name=file size=30>"+
"
"+
""
+
"<textarea name=\"bdata\" cols=\"70\" rows=\"20\">title|1998|28|209|name|b2|9483698|"+
"title|1998|14|93|name|b3|12766437"+
"</textarea>";
String open = "<textarea name=\"bdata\" cols=\"70\" rows=\"20\">";
String close = "</textarea>";
Pattern pattern = Pattern.compile("(?<="+open+")(.*?)(?="+close+")");
Matcher matcher = pattern.matcher(htmlContents);
String found = matcher.find() ? matcher.group() : null;
System.out.println(found);
while((str = in.readLine()) != null)
{
String open = "<textarea name=\"bdata\" cols=\"70\" rows=\"20\">";
String close = "</textarea>";
Pattern pattern = Pattern.compile("(?<="+open+")(.*?)(?="+close+")");
Matcher matcher = pattern.matcher(str);
String found = matcher.find() ? matcher.group() : null;
System.out.println(found);
}
Please do tell me why my output is showing as null
Simmya at 2007-7-12 20:00:19 >

...
...
...
<option value="PMC">PMC</option>
</select>
Email:
<Input type=text name=email value="">
File: <Input type=file name=file size=30>
<textarea name="bdata" cols="70" rows="20">title|1998|28|209|name|b2|9483698
title|1998|14|93|name|b3|12766437
...
...
...
From my above html file, I am unable to catch the line title|1998|28|209|name|b2|9483698
coming immediately after <textarea name="bdata" cols="70" rows="20">
. But I am able to catch all the line from title|1998|14|93|name|b3|12766437
onwards.
while((str = in.readLine()) != null)
{
if(str.substring(str.indexOf("<")+1).indexOf("textarea name=\"bdata\" cols=\"70\" rows=\"20\">")==0)
{
flag=true;
continue;
}
else if(str.substring(str.indexOf("<")+1).equals("</textarea>"))
{
flag=false;
break;
}
else if(flag && (str.length()>0))
{
...
}
}
Please tell me what modifications to do.
Simmya at 2007-7-12 20:00:19 >

> ...
> Please do tell me why my output is showing as
> null
That means matcher.find() returned false.
But you're reading your file line by line, which was not what I suggested. Like I wrote earlier: "... I suggest reading your html file in one String and then use some (simple) regex to macth for a pattern ...".
My output is null. Please tell me where am I wrong.
private String htmlContents="";
while((val = in.read()) != -1)
{
ch = (char)val;
htmlContents = htmlContents + ch;
}
String open = "<textarea name=\"bdata\" cols=\"70\" rows=\"20\">";
String close = "</textarea>";
Pattern pattern = Pattern.compile("(?<="+open+")(.*?)(?="+close+")");
Matcher matcher = pattern.matcher(htmlContents);
String found = matcher.find() ? matcher.group() : null;
System.out.println(found);
Simmya at 2007-7-12 20:00:19 >

Like I already said twice: read the file line by line (not byte by byte!): you can use the java.util.Scanner class for this.
Append each line from that Scanner object to a StringBuffer or StringBuilder.
When you're done reading the file, do something like this:String htmlContents = yourBuffer.toString();
now search your 'htmlContents' String for the pattern.
Good luck.