Home > JAVA > How to parse simple html page

How to parse simple html page

Short overview about this post. I needed to read data from very simple web server, parse them and provide the result to the jAppet. For better imagine I have posted small image around. Server was connected to the device.

Regulator_1

Also other limitation was to use only common Java libraries.  Output from web server looks like this :

<p>Temp0 =   8.0 Temp1 =   0.0</p>
<p>ID1=FFFFFFFFFFFFFF</p>
<p>ID2=FFFFFFFFFFFFFF</p>
<p>Scan_time=   5806</p>
<p>LOCK_TIME=  0</p>

My Java version was 1.6.0_16-b01. Java libraries I used for parsing are following.

import java.net.*;
import java.io.*;
import java.util.Scanner;

I’m intending to note only part of source codes to imagine how I did it.
Part1 – server connection

// Define string of the URL
 String urlStr = "http://<webserver address>";
 try {
 URL url = new URL(urlStr);
 URLConnection uc = url.openConnection();
 BufferedReader in = new BufferedReader(
 new InputStreamReader(
 uc.getInputStream()));
 String inputLine;
 while ((inputLine = in.readLine()) != null){ ..

As I read input over all lines this is function used for parsing all valuable lines …
Part2 – reading lines

 protected static void processLine(String pLine){
 //use a second Scanner to parse the content of each valuable line
 Scanner scanner = new Scanner(pLine);
 //define statement exp. Value=123
 scanner.useDelimiter("=");
 //eliminate tag <p> statement - exp. <p>Temp0 = ...
 scanner.skip("<p>");

 if ( scanner.hasNext() ){
 String name = scanner.next();
 String value = scanner.next();
 //Define end <p> tag statement and erase it exp. _time=    193</p>
 String value2  = value.substring(0, value.length()-4);    // Erase </p>
 System.out.println("Name = " + name.trim() + ", Value = " + value2.trim() );    // output test 1
 }
 else {
 System.out.println("Empty or invalid line. Unable to process.");
 }
 scanner.close();
 }

I hope this gonna be fitting your needs or just for information.

 

Advertisements
Categories: JAVA
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: