NishantKashyap
asked on
read the html table data using java.
I have a html source and I want to read the table data from it using java
for eg:-
</table>
</tr>
<tr><td>Test Studio Id</td>
<td>Debug Code</td>
<td>getResponse Code?</td>
<td>getResponse Message?</td>
</tr>
<tr><td>TC_001</td>
<td>DC002</td>
<td><span class="pass">EXCEPTION_DEC ODE_FAILED </span></t d>
<td><span class="ignore">Invalid serial number</span></td>
</tr>
I want to read the two values
1) "Test Studio Id" column values and corresponding
2) span class = 'pass' fromr "getResponse Code?" column.
Can anyone please help?
for eg:-
</table>
</tr>
<tr><td>Test Studio Id</td>
<td>Debug Code</td>
<td>getResponse Code?</td>
<td>getResponse Message?</td>
</tr>
<tr><td>TC_001</td>
<td>DC002</td>
<td><span class="pass">EXCEPTION_DEC
<td><span class="ignore">Invalid serial number</span></td>
</tr>
I want to read the two values
1) "Test Studio Id" column values and corresponding
2) span class = 'pass' fromr "getResponse Code?" column.
Can anyone please help?
ASKER
Thanks CEHJ.. but I already have a code to get the content(source) of HTML file using java, now I want to get the specific values from the table inside that source/html content.
ASKER
my code for reading the html file is :-
public class SourceViewerNew{
public static void main (String[] args) throws IOException{
System.out.print("Enter url of local for viewing html source code: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.i n));
String url = br.readLine();
try{
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
//System.out.println("HTTP /1.x " + code + " " + response);
for(int j = 1; ; j++){
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if(header == null || key == null)
break;
// System.out.println(uc.getH eaderField Key(j) + ": " + header);
}
InputStream in = new BufferedInputStream(uc.get InputStrea m());
Reader r = new InputStreamReader(in);
int c;
Writer output = null;
// File file = new File("C:\\SourceCode.txt") ;
//output = new BufferedWriter(new FileWriter(file));
StringBuffer sb = new StringBuffer();
while((c = r.read()) != -1){
//System.out.print((char)c );
// output.write((char)c);
sb.append((char)c);
}
now I want to search my query in the extracted html content.
public class SourceViewerNew{
public static void main (String[] args) throws IOException{
System.out.print("Enter url of local for viewing html source code: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.i
String url = br.readLine();
try{
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
//System.out.println("HTTP
for(int j = 1; ; j++){
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if(header == null || key == null)
break;
// System.out.println(uc.getH
}
InputStream in = new BufferedInputStream(uc.get
Reader r = new InputStreamReader(in);
int c;
Writer output = null;
// File file = new File("C:\\SourceCode.txt")
//output = new BufferedWriter(new FileWriter(file));
StringBuffer sb = new StringBuffer();
while((c = r.read()) != -1){
//System.out.print((char)c
// output.write((char)c);
sb.append((char)c);
}
now I want to search my query in the extracted html content.
You'd need to reparse the collected html if you want to do it that way
Change the example from
>>Reader rd = new InputStreamReader(conn.get InputStrea m());
to
>>Reader rd = new InputStreamReader(conn.get
to
Reader rd = new InputStreamReader(new StringReader(sb.toString()));
you can adapt this example
http://helpdesk.objects.com.au/java/how-do-i-extract-just-the-text-form-a-html-document-ie-strip-out-all-the-html-tags
or using something like htmlunit
http://htmlunit.sourceforge.net/gettingStarted.html
http://helpdesk.objects.com.au/java/how-do-i-extract-just-the-text-form-a-html-document-ie-strip-out-all-the-html-tags
or using something like htmlunit
http://htmlunit.sourceforge.net/gettingStarted.html
ASKER
Thanks for the reply but still my original question is not answered... how do I read the table data ?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
but for something more complex, use a fuller-featured parser