Solved

read the html table data using java.

Posted on 2010-11-16
8
1,661 Views
Last Modified: 2013-11-23
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_DECODE_FAILED</span></td>
<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?
0
Comment
Question by:NishantKashyap
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34143863
For simple html:  http://www.exampledepot.com/egs/javax.swing.text.html/GetText.html
but for something more complex, use a fuller-featured parser
0
 

Author Comment

by:NishantKashyap
ID: 34143890
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.
0
 

Author Comment

by:NishantKashyap
ID: 34143898
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.in));
    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.getHeaderFieldKey(j) + ": " + header);
      }
      InputStream in = new BufferedInputStream(uc.getInputStream());
      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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34143919
You'd need to reparse the collected html if you want to do it that way
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 34143930
Change the example from

>>Reader rd = new InputStreamReader(conn.getInputStream());


to
Reader rd = new InputStreamReader(new StringReader(sb.toString()));

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34143978
0
 

Author Comment

by:NishantKashyap
ID: 34144148
Thanks for the reply but still my original question is not answered... how do I read the table data ?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34144327
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
wordmultiple challenge 12 116
Java and GPO 11 66
Java SE 8u111  Lot of stuff broke 11 52
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now