Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Utility XML_Parser  

XML_Parser

XML Parser Test script

Category: Utility
By : kagefumi
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for XML_Parser
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. XML_Parser_1.lsl
Get file # 2. XML_Parser_2.lsl
1 xpHandleStartElement(string name, list attrs)
2 {
3 string str = "START_TAG: " + name;
4 integer i;
5 for (i = 0; i < llGetListLength(attrs); i += 2)
6 {
7 str = str
8 + " "
9 + llList2String(attrs, i)
10 + "="
11 + llList2String(attrs, i + 1);
12 }
13 llSay(0, str);
14 }
15
16 xpHandleText(string text)
17 {
18 llSay(0, "TEXT: " + text);
19 }
20
21 xpHandleEndElement(string name)
22 {
23 llSay(0, "END_TAG: " + name);
24 }
25
26 xpHandleComment(string content)
27 {
28 llSay(0, "COMMENT: " + content);
29 }
30
31 xpHandleCDATA(string content)
32 {
33 llSay(0, "CDATA: " + content);
34 }
35
36 default
37 {
39 {
40 xpParseXML("<a><b b1='ll' b2='mm nn'><c c1='oo=pp qq ' c2='='/><!-- ddd --></b></a><![CDATA[ eee < fff > ggg]]>");
41 xpParseXML("<x><y y1=\"rr ss\">uuuu<z/>vvvv</y></x>");
42 }
43 }

XML_Parser

XML Parser Main Script

Category: Utility
By : kagefumi
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 //===============================================
2 //
3 // XML Parser.
4 //
5 //===============================================
6
7 //
8 // Parses XML.
9 //
10 xpParseXML(string html)
11 {
12 list tokens = llParseString2List(html, ["<"], []);
13 integer tokens_size = llGetListLength(tokens);
14 integer i;
15 string content = "";
16 string end_token = "";
17 for (i = 0; i < tokens_size; ++i)
18 {
19 string token = llList2String(tokens, i);
20 if(end_token == "")
21 {
22 if(llSubStringIndex(token, "!--") == 0)
23 // Comment Start.
24 {
25 end_token = "-->";
26 integer end_index = llSubStringIndex(token, end_token);
27 content = llGetSubString(token, 3, end_index);
28 if(0 <= end_index)
29 {
30 xpHandleComment(content);
31 end_token = "";
32 content = "";
33 }
34 }
35 else if(llSubStringIndex(token, "![CDATA[") == 0)
36 // CDATA Start.
37 {
38 end_token = "]]>";
39 integer end_index = llSubStringIndex(token, end_token);
40 content = llGetSubString(token, 8, end_index);
41 if(0 <= end_index) {
42 end_token = "";
43 content = "";
44 }
45 }
46 else if(llSubStringIndex(token, "?") == 0)
47 // PI Start.
48 {
49 end_token = "?>";
50 integer end_index = llSubStringIndex(token, end_token);
51 content = llGetSubString(token, 1, end_index);
52 if(0 <= end_index) {
53 end_token = "";
54 content = "";
55 }
56 }
57 else if(llSubStringIndex(token, "/") == 0)
58 // Element End
59 {
60 string name = "";
61 string text = "";
62 list elem_text = llParseString2List(token, [">"], []);
63 name = llList2String(elem_text, 0);
64 name = llGetSubString(name, 1, -1);
65 xpHandleEndElement(name);
66 if(llGetListLength(elem_text) == 2)
67 {
68 text = llList2String(elem_text, 1);
69 if(0 < llStringLength(text))
70 {
71 xpHandleText(text);
72 }
73 }
74 }
75 else
76 // Element Start
77 {
78 string name = "";
79 string text = "";
80 list attrs = [];
81 // "elem>text" -> [elem, text]
82 list elem_text = llParseString2List(token, [">"], []);
83 string elem = llList2String(elem_text, 0);
84 if(llGetListLength(elem_text) == 2)
85 {
86 text = llList2String(elem_text, 1);
87 }
88 // "name attr1=value1 attr2=value2" -> [name, attr1=value1, attr2=value2]
89 list etokens = llParseString2List(elem, [" "], []);
90 name = llList2String(etokens, 0);
91 string quote = "";
92 string attr_name = "";
93 string attr_value = "";
94 integer etokens_size = llGetListLength(etokens);
95 integer j;
96 for (j = 1; j < etokens_size; ++j)
97 {
98 string etoken = llList2String(etokens, j);
99 if(quote == "")
100 {
101 // "attr1=value1" -> [attr1, value1]
102 list atokens = llParseString2List(etoken, ["="], []);
103 integer atokens_size = llGetListLength(atokens);
104 if(2 <= atokens_size)
105 {
106 attr_name = llList2String(atokens, 0);
107 attr_value = llList2String(atokens, 1);
108 quote = llGetSubString(attr_value, 0, 0);
109 if(quote == "\"" || quote == "'")
110 {
111 if(2 <= llStringLength(attr_value))
112 {
113 attr_value = llGetSubString(attr_value, 1, -1);
114 }
115 else
116 {
117 attr_value = "";
118 }
119 }
120 else
121 {
122 quote = "";
123 }
124 integer k;
125 for (k = 2; k < atokens_size; ++k)
126 {
127 attr_value += "=" + llList2String(atokens, k);
128 }
129 if(quote == "\"" || quote == "'")
130 {
131 integer attr_value_size = llStringLength(attr_value);
132 if(1 <= attr_value_size && llSubStringIndex(attr_value, quote) == attr_value_size - 1)
133 {
134 if(2 <= attr_value_size)
135 {
136 attr_value = llGetSubString(attr_value, 0, attr_value_size - 2);
137 }
138 else
139 {
140 attr_value = "";
141 }
142 attrs += attr_name;
143 attrs += attr_value;
144 attr_name = "";
145 attr_value = "";
146 quote = "";
147 }
148 }
149 else
150 {
151 attrs += attr_name;
152 attrs += attr_value;
153 attr_name = "";
154 attr_value = "";
155 quote = "";
156
157 }
158 }
159 else if(atokens_size == 1)
160 {
161 attr_name = llList2String(atokens, 0);
162 attr_value = "";
163 attrs += attr_name;
164 attrs += "";
165 attr_name = "";
166 attr_value = "";
167 quote = "";
168 }
169 }
170 else
171 {
172 attr_value += " " + etoken;
173 integer attr_value_size = llStringLength(attr_value);
174 if(1 <= attr_value_size && llSubStringIndex(attr_value, quote) == attr_value_size - 1)
175 {
176 if(2 <= attr_value_size)
177 {
178 attr_value = llGetSubString(attr_value, 0, attr_value_size - 2);
179 }
180 else
181 {
182 attr_value = "";
183 }
184 attrs += attr_name;
185 attrs += attr_value;
186 attr_name = "";
187 attr_value = "";
188 quote = "";
189 }
190 }
191 }
192 xpHandleStartElement(name, attrs);
193 if(llGetSubString(elem, -1, -1) == "/")
194 {
195 xpHandleEndElement(name);
196 }
197 if(0 < llStringLength(text))
198 {
199 xpHandleText(text);
200 }
201 }
202 }
203 else
204 {
205 integer end_index = llSubStringIndex(token, end_token);
206 if(0 <= end_index)
207 {
208 if(1 <= end_index)
209 {
210 content += "<" + llGetSubString(token, 0, end_index - 1);
211 }
212 if(end_token == "-->")
213 {
214 xpHandleComment(content);
215 }
216 else if(end_token == "]]>")
217 {
218 xpHandleCDATA(content);
219 }
220 else if(end_token == "?>")
221 {
222 //xpHandlePI(content);
223 }
224 else
225 {
226 }
227 end_token = "";
228 content = "";
229 }
230 else
231 {
232 content += token;
233 }
234 }
235 }
236 }
237
238 xpHandleStartElement(string name, list attrs)
239 {
240 string str = "START_TAG: " + name;
241 integer i;
242 for (i = 0; i < llGetListLength(attrs); i += 2)
243 {
244 str = str
245 + " "
246 + llList2String(attrs, i)
247 + "="
248 + llList2String(attrs, i + 1);
249 }
250 llSay(0, str);
251 }
252
253 xpHandleText(string text)
254 {
255 llSay(0, "TEXT: " + text);
256 }
257
258 xpHandleEndElement(string name)
259 {
260 llSay(0, "END_TAG: " + name);
261 }
262
263 xpHandleComment(string content)
264 {
265 llSay(0, "COMMENT: " + content);
266 }
267
268 xpHandleCDATA(string content)
269 {
270 llSay(0, "CDATA: " + content);
271 }

Back to the Best Free Tools in Second Life and OpenSim.