maybe here is someone who knows FANUC KAREL a little bit.
I have to create a XML parser, to get specific parts of a telegram.
I started with the FANUC template code, but currently I get no results. Nothing.
Here you can see the message, I have to parse:
<?xml version="1.0" ?>
<message><name>Run.Locate.Ok</name><job>1</job><match>1</match><matches>1</matches><x>90.735872311523</x><y>-5.0591040552784</y><z>0</z><rx>0</rx><ry>0</ry><rz>166.37113177833</rz><px>90.735872311523</px><py>-5.0591040552784</py><pz>0</pz><prx>0</prx><pry>0</pry><prz>166.37113177833</prz><scale>1.0</scale><score>97.627884149551</score><time>134</time><exposure>1</exposure><identified>1</identified></message>
And here you can see my current KAREL source:
PROGRAM xmlparse
%NOLOCKGROUP
%NOPAUSESHFT
%NOPAUSE = ERROR + COMMAND + TPENABLE
%NOABORT = ERROR + COMMAND
%ENVIRONMENT xml
%include klerxmlf
CONST
MYXML_CONST = 3
TYPE
xmlstrct_t = STRUCTURE
first : integer
second : real
third : boolean
fourth : string[20]
ENDSTRUCTURE
-- Local Vars
VAR
xml_result : file
status : INTEGER
tag_name : string[32]
attrnames : array[32] of string[32]
attrvalues : array[32] of string[64]
text : array[32] of String[128]
xmlstrct : xmlstrct_t
tag_ident : integer
textdone : boolean
func_code : integer
text_idx : integer
numattr : integer
done : boolean
startdata : boolean
xmlp_done : boolean
Routine xml_parse:boolean
BEGIN
-- Parse XML Data
SET_FILE_ATR(xml_result, ATR_XML)
CLR_IO_STAT(xml_result)
WRITE TPDISPLAY('XML_Start',CR)
OPEN FILE xml_result('RO','MC:result.xml')
WRITE TPDISPLAY('File OK',CR)
status = IO_STATUS(xml_result)
WRITE TPDISPLAY('Status 1',CR)
IF (status <> 0) THEN
POST_ERR(status, '', 0, 0)
abort
ENDIF
WRITE TPDISPLAY('Status 2',CR)
xml_addtag (xml_result, 'xmlstrct_t', 32, FALSE, MYXML_CONST, status)
WRITE TPDISPLAY('Status 3',CR)
textdone = TRUE
done = FALSE
startdata = FALSE
while (done = FALSE) DO
xml_scan(xml_result,'message',tag_ident,func_code,status)
WRITE TPDISPLAY('Status 4',CR)
if (status = 0) THEN
done = TRUE
ENDIF
IF (status = XML_FUNCTION) THEN
Status = 0
SELECT tag_ident of
Case (MYXML_CONST) :
SELECT func_code of
Case (XML_START) :
text_idx = 1
xml_setvar (xml_result, 'message', 'xmlstrct', status)
xml_getdata(xml_result, numattr, attrnames, attrvalues, text[text_idx], textdone, status)
Case (XML_END) :
IF (startdata = TRUE) THEN
startdata = FALSE
ELSE
text_idx = 1
xml_setvar (xml_result, 'message', 'xmlstrct', status)
xml_getdata(xml_result, numattr, attrnames, attrvalues, text[text_idx], textdone, status)
ENDIF
Case (XML_TXCONT) :
text_idx = text_idx + 1
xml_getdata (xml_result, numattr, attrnames, attrvalues, text[text_idx], textdone, status)
ELSE:
ENDSELECT
ELSE:
ENDSELECT
ELSE
IF (status <> XML_SCANLIM) THEN
POST_ERR(status, '', 0, 0)
done = TRUE
ENDIF
ENDIF
ENDWHILE
WRITE TPDISPLAY('Status 5',CR)
xml_remtag(xml_result, 'xmlstrct_t', status)
close file xml_result
status = IO_STATUS(xml_result)
IF (status <> 0) THEN
POST_ERR(status, '', 0, 0)
xmlp_done = FALSE
ELSE
xmlp_done = TRUE
ENDIF
WRITE TPDISPLAY('File Closed 3',CR)
RETURN (xmlp_done)
END xml_parse
Begin
END xmlparse
What I need is the seperated Tag contents, like the position.
Can anybody give me some brain input?
Thanks!
2
Answers
FANUC Karel manual is definitely unclear about the XML functions. I wrote a Karel XML parser only once, 5 years ago, so I cannot be as accurate as I would, but I clearly remember a few hints that may help you.
For first, try a one line XML file and write a specific Karel program for it, it will help you a lot in understanding how Karel works.
Keep in mind that a STRUCTURE should describe exactly the same type and quantity of data that the XML tags will contain: the structure of the manual example is made for an XML approx made like
In your case, let me format your XML:
I’d rather go for a more pragmatic approach:
And than use directly the XML functions without the loops overheads.
A small hint: FANUC robots uses a 32 bit single precision representation for real numbers; if possible, I’d rather use a shorter representation for floats, otherwise the Karel program will heavily round the values and you may miss some relevant data.
Hope this helps, in case let me know that I can try to search my Karels.
I think the problem might be:
Should be:
RW instead of RO when opening the XML file