2017-04-19 28 views
0

嗨我是新手编程,需要为我的学校项目提取数据。数据来自Arduino设备,该设备发送的数据看起来像这样。在netbeans中分割尖括号之间的传入数据

<call_sign>IVV-HAM</call_sign> 
<mission_call>ARP</mission_call> 
<absolute_time>13:13:13</absolute_time> 
<elapsed_time>43</elapsed_time> 
<imu_accel_x>240</imu_accel_x> 
<imu_accel_y>196</imu_accel_y> 
<imu_accel_z>62</imu_accel_z> 
<imu_gyro_x>599</imu_gyro_x> 
<imu_gyro_y>1013</imu_gyro_y> 
<imu_gyro_z>215</imu_gyro_z> 
<imu_mag_x>402</imu_mag_x> 
<imu_mag_y>495</imu_mag_y> 
<imu_mag_z>447</imu_mag_z> 
<imu_temp>453</imu_temp> 

我需要摆脱前三行,然后发送剩余的代码进行绘图。我试图划分和分割尖括号但我知道怎么做似乎工作。 任何帮助意见或建议我可以找到答案(我已经在这里看了一天已经),将不胜感激谢谢。

public void run(){ 
        //create a new Scanner and connect it to the Serial Port 
        input = new Scanner(connectedPort.getInputStream()); 
        //HERE I tried to use input.useDelimiter("></") 
        // I also tried to split by creating a string.split but 
        // it did not work either. 
        //loop indefinitely until you get data from the Serial Port 
        while(input.hasNextLine()){ 
         //get line of text from serial port 
         String line = input.nextLine(); 

         //dump the raw data to the output text area 
         //taRawOutput.append(Integer.toString(number)+'\n'); 
         textArea_RawData.append(line+"\n"); 
        }//end of loop 

        //close the scanner object reading from the serial port 
        input.close(); 
       }//end of methd run() 
      };//end of inner class definition 

      //start the thread that was just defined running 
      thread.start(); 

     }catch(IOException e){ 
      JOptionPane.showMessageDialog(this, e.getMessage()); 
     }//end of catch block 
    } 

我试着编辑问题来显示我做了什么。

+0

请告诉我们你试过的东西 –

+0

PS看起来像XML –

+0

“新扫描仪”之后,我尝试使用“input.useDelimiter(”>

回答

1

您需要在之前将拆分为一个开放式括号,但不要在其后面加斜杠。你需要拆分不消耗括号。所有这些都可以通过展望完成。

试试这个:

scanner.useDelimiter("(?=<[^/])"); 

然后每次调用时scanner.next(),你会在整个标签读取。

正则表达式的意思是“以下字符是一个<然后一个字符不是斜杠”,它不会消耗任何输入的(匹配是零长度的,实际上字符之间)

+1

没有解释,只是一个先进的正则表达式抛出新手。严肃的问题:如果你在主持人角色中遇到这样的答案,你会怎么做? – GhostCat

+1

@GhostCat在你跳到结论之前,我在一个非常糟糕的移动接收区域,无法发布解释部分(保持超时)。我在帖子上的第一次尝试死了,所以我再次尝试(从头开始)。现在我回来了,我已经发布了一个正确的答案。如果我看到原稿,我可能会要求海报来解释它。 – Bohemian

+0

谢谢。说得通。 – GhostCat

0

我决定放弃一个分隔符,并使用带有“indexOf(”>“)+ 1,lastIndexOf(”<“)的Vector,我知道这已经过时了,但它适用于我的目的。它拥有我需要的数据,并可以发送到我需要的不同图表。 谢谢你的帮助,虽然我很感激。