html放入一个String中,将每行<td class = "b"></td>中的值读出

我现在有一个网页。
<html>
<body>
<div>31423423423
</div>
<div>
<table>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">12</td>
</tr>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">123434</td>
</tr>
<tr>
<td class = "a">asdfdas</td><th>234</th><td class = "b">132</td>
</tr>
</table>
</div>
</body>
</html>

 

import java.util.*;
import java.io.*;
import java.util.regex.*;

public class TableTest {

public static void main(String[] args) {
String rex = "<\\s*td\\s+class\\s+=\\s+\"\\s*b\\s*\"\\s*>(.*)<\\s*/td\\s*>";
//指定要提取的网页
String path = "c:/test.html";
Pattern p = Pattern.compile(rex);
Matcher m = null;
StringBuffer sb = new StringBuffer();
String temp = null;
try {
File f = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
while((temp = reader.readLine()) != null) {
m = p.matcher(temp);
while(m.find()) {
System.out.println(m.group(1));
sb.append(m.group(1) + "!");
}
}
} catch (Exception e) {
}
/////res就是保存的结果
/*
String [] res = sb.toString().split("!");
for(String s: res) {
System.out.println(s);
}
*/
}
}

package com.intl.bluetoothhumiture; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.widget.TextView; import java.text.DecimalFormat; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private static final int MAXBUFFLEN = 1024; private static final int INTERVAL4PROCESSINGBUFFDATA = 100; private Timer mTimer; private Handler mHandler; private boolean bDataLock = false; private int iDataIn = 0; private int iDataOut = 0; private byte[] bytesDataRecBuff = new byte[MAXBUFFLEN]; private Service_Socket mService; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = ((Service_Socket.LocalBinder) service).getService(); Service_Socket.strMessageFor = TAG; //切换串口 new Timer().schedule(new TimerTask() { public void run() { switchSerial(1, 115200); } }, 100); //切换模块 new Timer().schedule(new TimerTask() { public void run() { switchModule(1); } }, 200); } @Override public void onServiceDisconnected(ComponentName name) { mService = null; } }; private ServiceMsgReceiver mServiceMsgRecv; public class ServiceMsgReceiver extends BroadcastReceiver { //*******************代码补全****************************** } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initEnv(); bindService(new Intent(this, Service_Socket.class), mConnection, Context.BIND_AUTO_CREATE); } @Override public void onResume() { Service_Socket.strMessageFor = TAG; super.onResume(); } @Override public void onDestroy() { if (mTimer != null) { mTimer.cancel(); mTimer = null; } super.onDestroy(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(MainActivity.this).setIcon(R.mipmap.ic_launcher).setTitle("退出").setMessage("确认退出吗?") .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialoginterface, int i) { finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialoginterface, int i) { } }).show(); return true; } return super.onKeyDown(keyCode, event); } //初始化 private void initEnv() { //初始化数据缓冲标志 bDataLock = false; iDataIn = 0; iDataOut = 0; // 注册广播接收器****************代码补全******************************** mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: processingData(); break; } super.handleMessage(msg); } }; mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { Message msg = mHandler.obtainMessage(); msg.what = 1; mHandler.sendMessage(msg); } }, 500, INTERVAL4PROCESSINGBUFFDATA); } //接收数据到数据缓冲区内 private void receiveData(byte[] bRecData) { //Luo Test //Log.i(TAG + ":Data", bytes2HexString(bRecData)); int i; int iDataLen = bRecData.length; if (bDataLock == false) { bDataLock = true; if (iDataIn + iDataLen <= MAXBUFFLEN) { for (i = 0; i < iDataLen; i++) { bytesDataRecBuff[iDataIn + i] = bRecData[i]; } iDataIn += iDataLen; } else { for (i = iDataIn; i < MAXBUFFLEN; i++) { bytesDataRecBuff[i] = bRecData[i - iDataIn]; } for (i = 0; i < iDataLen - MAXBUFFLEN + iDataIn; i++) { bytesDataRecBuff[i] = bRecData[i + MAXBUFFLEN - iDataIn]; } iDataIn = iDataLen - MAXBUFFLEN + iDataIn; } bDataLock = false; } } //读取当前缓冲区中的数据 private int validReceiveLen() { if (iDataOut < iDataIn) { return (iDataIn - iDataOut); } else if (iDataOut > iDataIn) { return (MAXBUFFLEN - iDataOut + iDataIn); } return 0; } //从缓冲区内读出有效数据 private void processingData() { if (bDataLock == false) { bDataLock = true; int iPacketLen = 0; int i, iValidLen, iReadPos; while (iDataIn != iDataOut) { if (bytesDataRecBuff[iDataOut] == (byte) 0xAA) {// 判断是否为包头 iValidLen = validReceiveLen();// 包含有效数据长度 if (iValidLen < 8) { // 有效长度太短 bDataLock = false; return; } if (iDataOut + 1 >= MAXBUFFLEN) { iPacketLen = bytesDataRecBuff[iDataOut + 1 - MAXBUFFLEN] & 0xFF; } else { iPacketLen = bytesDataRecBuff[iDataOut + 1] & 0xFF; } if (iValidLen < iPacketLen) { // 包不完整 bDataLock = false; return; } if (iPacketLen > 7 && iPacketLen < 40) { // 数据长度正常 iReadPos = iDataOut; byte[] buf = new byte[iPacketLen]; for (i = 0; i < iPacketLen; i++) {// 读出包并进行校验和计算 buf[i] = bytesDataRecBuff[iReadPos++]; if (iReadPos >= MAXBUFFLEN) iReadPos = 0; } if (checkSummationVerify(buf)) { iDataOut = iReadPos; processingPacket(buf); bDataLock = false; return; } } } iDataOut++; if (iDataOut >= MAXBUFFLEN) { iDataOut = 0; } } bDataLock = false; } } //处理接收的数据包 private void processingPacket(byte[] Packet) { //Luo Test Log.i(TAG + ":Packet", bytes2HexString(Packet)); if (Packet[2] == (byte) 0x02) {//无线传感 if (Packet[4] == (byte) 0x05) {//上传传感器采集数据 if (Packet[5] == (byte) 0x03) {//Bluetooth byte bSensorType = Packet[7]; byte bSensorIndex = Packet[8]; byte[] bytes = new byte[Packet.length - 10]; for (int i = 0; i < bytes.length; i++) { bytes[i] = Packet[9 + i]; } switch (bSensorType) { case 0x02://温湿度 TextView txtInfo = (TextView) findViewById(R.id.txtInfo); int iTemp = (bytes[1] & 0xFF) * 256 + (bytes[2] & 0xFF); int iHumi = (bytes[3] & 0xFF) * 256 + (bytes[4] & 0xFF); DecimalFormat formater = new DecimalFormat("#0.0"); String str = "温度:" + formater.format(iTemp / 100.0) + " ℃\n湿度:" + formater.format(iHumi / 100.0) + " %"; txtInfo.setText(str); break; } } } } } //获取校验结果 private boolean checkSummationVerify(byte[] bytes) { byte b = 0x00; for (int i = 0; i < bytes.length - 1; i++) { b += bytes[i]; } if (bytes[bytes.length - 1] == b) { return true; } return false; } //设置校验位 private void setSummationVerify(byte[] bytes) { byte b = 0x00; for (int i = 0; i < bytes.length - 1; i++) { b += bytes[i]; } bytes[bytes.length - 1] = b; } //二进制数组转字符串 private String bytes2HexString(byte[] bytes) { String ret = ""; for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } //切换串口设置 public void switchSerial(int iWhich, int iBaudrate) { byte[] bytes = new byte[4]; bytes[0] = (byte) 0x4C; bytes[1] = (byte) 0x4B; bytes[2] = (byte) iWhich; switch (iBaudrate) { case 4800: bytes[3] = (byte) 0x00; break; case 9600: bytes[3] = (byte) 0x01; break; case 38400: bytes[3] = (byte) 0x02; break; case 57600: bytes[3] = (byte) 0x03; break; case 115200: bytes[3] = (byte) 0x04; break; } mService.socketSend(bytes); } //切换模块连接 public void switchModule(int iWhich) { byte[] bytes = new byte[7]; bytes[0] = (byte) 0xAA; bytes[1] = (byte) bytes.length; bytes[2] = (byte) 0x01; bytes[3] = (byte) 0x00; bytes[4] = (byte) 0x01; bytes[5] = (byte) iWhich; setSummationVerify(bytes); mService.socketSend(bytes); } //发送控制命令(带一个参数) private void sendControlCmd(byte bWsnType, byte bCtrlerType, byte bIndex, byte bValue) { byte[] bytes = new byte[12]; bytes[0] = (byte) 0xAA; bytes[1] = (byte) bytes.length; bytes[2] = (byte) 0x02; bytes[3] = (byte) 0x00; bytes[4] = (byte) 0x0F; bytes[5] = bWsnType; bytes[6] = (byte) 0x00; bytes[7] = bCtrlerType; bytes[8] = bIndex; bytes[9] = (byte) 0x01; bytes[10] = bValue; setSummationVerify(bytes); mService.socketSend(bytes); } //发送控制命令(带两个参数) private void sendControlCmd(byte bWsnType, byte bCtrlerType, byte bIndex, byte bValue1, byte bValue2) { byte[] bytes = new byte[13]; bytes[0] = (byte) 0xAA; bytes[1] = (byte) bytes.length; bytes[2] = (byte) 0x02; bytes[3] = (byte) 0x00; bytes[4] = (byte) 0x0F; bytes[5] = bWsnType; bytes[6] = (byte) 0x00; bytes[7] = bCtrlerType; bytes[8] = bIndex; bytes[9] = (byte) 0x02; bytes[10] = bValue1; bytes[11] = bValue2; setSummationVerify(bytes); mService.socketSend(bytes); } //发送控制命令(带三个参数) private void sendControlCmd(byte bWsnType, byte bCtrlerType, byte bIndex, byte bValue1, byte bValue2, byte bValue3) { byte[] bytes = new byte[14]; bytes[0] = (byte) 0xAA; bytes[1] = (byte) bytes.length; bytes[2] = (byte) 0x02; bytes[3] = (byte) 0x00; bytes[4] = (byte) 0x0F; bytes[5] = bWsnType; bytes[6] = (byte) 0x00; bytes[7] = bCtrlerType; bytes[8] = bIndex; bytes[9] = (byte) 0x03; bytes[10] = bValue1; bytes[11] = bValue2; bytes[12] = bValue3; setSummationVerify(bytes); mService.socketSend(bytes); } } 补全代码并输出完整代码
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值