1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/********************************************************
This sample demonstrates how to use the XMLParser
class to parse an XML communication protocol and
update model objects accordingly.
********************************************************/
import "XMLParser"
enum ObjectType
{
plane, truck, human
};
class Track
{
String id;
ObjectType type;
String description;
Vector3Df position;
Degrees direction;
float speed;
~Track()
{
delete id;
delete description;
}
}
List<Track> tracks { };
TrackXMLParser parser { };
class TrackingSocket : Socket
{
unsigned int OnReceive(const byte * buffer, unsigned int count)
{
parser.Parse((char *)buffer, count);
// Update Things Here
return count;
}
}
class TrackXMLParser : XMLParser
{
Track object;
void ProcessKeyword(const char * keyWord)
{
if(!strcmpi(keyWord, "object"))
{
char * id = null;
ObjectType type = 0;
char * desc = null;
while(GetWord())
{
if(!strcmpi(keyWord, "id")) { GetWord(); id = CopyString(keyWord); }
else if(!strcmpi(keyWord, "type")) { GetWord(); type = (ObjectType)atoi(keyWord); }
else if(!strcmpi(keyWord, "desc")) { GetWord(); desc = CopyString(keyWord); }
}
for(object : tracks)
{
if(!strcmp(object.id, id)) break;
}
if(object)
{
object.type = type;
object.description = desc;
}
else
{
tracks.Add({ id = id, type = type, description = desc });
}
delete desc;
}
else if(!strcmpi(keyWord, "/object"))
{
object = null;
}
else if(!strcmpi(keyWord, "pos"))
{
Vector3Df position { };
while(GetWord())
{
if(!strcmpi(keyWord, "x")) { GetWord(); position.x = (float)atof(keyWord); }
else if(!strcmpi(keyWord, "y")) { GetWord(); position.y = (float)atof(keyWord); }
else if(!strcmpi(keyWord, "z")) { GetWord(); position.z = (float)atof(keyWord); }
else if(!strcmpi(keyWord, "speed")) { GetWord(); object.speed = (float)atof(keyWord); }
else if(!strcmpi(keyWord, "dir")) { GetWord(); object.direction = (float)atof(keyWord); }
}
object.position = position;
}
}
};
enum MyTag
{
none,
myTag1,
myTag2,
myTag3
};
class MyParser : XMLParser
{
MyTag tag;
void ProcessCharacterData(const char * data)
{
switch(tag)
{
case myTag1: PrintLn("myTag1: ", data); break;
case myTag2: PrintLn("myTag2: ", data); break;
case myTag3: PrintLn("myTag3: ", data); break;
}
}
void ProcessKeyword(const char * keyWord)
{
NamedLink64 nl;
EnumClassData tagData = class(MyTag).data;
for(nl = tagData.values.first; nl; nl = nl.next)
{
if(!strcmpi(keyWord, nl.name))
{
MyTag curTag = (MyTag)nl.data;
if(openingTag)
tag = curTag;
else
tag = none;
break;
}
}
}
}
|