I have an eye-tracker uses TCP/IP communication and XML to send data between client (application) and server (eye-tracker). The following is an example of the XML data string I receive continuously while the eye-tracker is on. What I would like to do is be able to use the data, FPOGX and FPOGY, as an input to another function I have. The problem is they are not variables and you can't just simply call upon them. How do I parse this data stream? This is the first time I've worked with XML. Examples would be greatly appreciated. Thanks!
CLIENT SEND: <SET ID="ENABLE_SEND_COUNTER" STATE="1" />
SERVER SEND: <ACK ID="ENABLE_SEND_COUNTER" STATE="1" />
CLIENT SEND: <SET ID="ENABLE_SEND_POG_FIX" STATE="1" />
SERVER SEND: <ACK ID="ENABLE_SEND_POG_FIX" STATE="1" />
CLIENT SEND: <SET ID="ENABLE_SEND_DATA" STATE="1" />
SERVER SEND: <ACK ID="ENABLE_SEND_DATA" STATE="1" />
SERVER SEND: <REC CNT="72" FPOGX="0.5065" FPOGY="0.4390"
FPOGD="0.078" FPOGID="468" FPOGV="1"/>
SERVER SEND: <REC CNT="73" FPOGX="0.5071" FPOGY="0.4409"
FPOGD="0.094" FPOGID="468" FPOGV="1"/>
SERVER SEND: <REC CNT="74" FPOGX="0.5077" FPOGY="0.4428"
FPOGD="0.109" FPOGID="468" FPOGV="1"/>
Here is a snippet of some parts of the code:
import xml.etree.cElementTree as ET
import cv2
import cv
import socket
# Code to grab different data from eye-tracker
'...'
# Code to create window and initialize camera
'...'
def xmlParse():
rxdat = s.recv(1024) # Syntax from eye-tracker to grab XML data stream of <REC />
if(rxdat.find("ACK") == 1): # First two XML have the <ACK /> tag but I don't need those
pass
else: # Here is the part where it parses and converts the data to float
rxdat = '<data>' + rxdat + '</data>'
xml = ET.fromstring(rxdat)
for element in xml:
X = float(xml[0].attrib['FPOGX'])
Y = float(xml[0].attrib['FPOGY'])
return (X, Y)
# Def to average samples of incoming X and Y
'...'
# Def that uses xmlParse() and average() to return the averages of X and Y
'...'
# Def for mouse click events
'...'
# Some code that makes our window graphics
'...'
for i in range(0,2): # Round-about way to get rid of the first two "NoneType"
xmlParse()
while True:
Img = cv.QueryFrame(capture) # capture defined earlier
drawarrow(polyF, polyB, polyL, polyR) # Our window graphics definition
cv.ShowImage("window", Img)
(X, Y) = gazeCoordinates() # Def that uses xmlParse and average to return the averages of X and Y
if cv.WaitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
The error given is ParseError: not well-formed (invalid token)
and points to the xml = ET.fromstring(rxdat)
of the code
The definition xmlParse() by itself and just printing out the results works. But once I start adding in the windows, graphics, and using the data, it starts giving out that error.