| function tree = xml_parser(xmlstr) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| global xmlstring Xparse_count xtree; |
|
|
| |
| |
| if isempty(xmlstr) |
| error('[XML] Not enough parameters.') |
| elseif ~ischar(xmlstr) || sum(size(xmlstr)>1)>1 |
| error('[XML] Input must be a string.') |
| end |
|
|
| |
| Xparse_count = 0; |
|
|
| |
| xmlstring = normalize(prolog(xmlstr)); |
|
|
| |
| xtree = {}; |
| tree = fragment; |
| tree.str = 1; |
| tree.parent = 0; |
|
|
| |
| tree = compile(tree); |
|
|
| |
| tree = xtree; |
|
|
| |
| clear global xmlstring Xparse_count xtree; |
|
|
| |
| |
|
|
| |
| function frag = compile(frag) |
| global xmlstring xtree Xparse_count; |
| |
| while 1, |
| if length(xmlstring)<=frag.str || ... |
| (frag.str == length(xmlstring)-1 && strcmp(xmlstring(frag.str:end),' ')) |
| return |
| end |
| TagStart = xml_findstr(xmlstring,'<',frag.str,1); |
| if isempty(TagStart) |
| |
| error('[XML] Unknown data at the end of the XML file.'); |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = chardata; |
| xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:end))); |
| xtree{Xparse_count}.parent = frag.parent; |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| frag.str = ''; |
| elseif TagStart > frag.str |
| if strcmp(xmlstring(frag.str:TagStart-1),' ') |
| |
| frag.str = TagStart; |
| else |
| |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = chardata; |
| xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:TagStart-1))); |
| xtree{Xparse_count}.parent = frag.parent; |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| frag.str = TagStart; |
| end |
| else |
| if strcmp(xmlstring(frag.str+1),'?') |
| |
| frag = tag_pi(frag); |
| else |
| if length(xmlstring)-frag.str>4 && strcmp(xmlstring(frag.str+1:frag.str+3),'!--') |
| |
| frag = tag_comment(frag); |
| else |
| if length(xmlstring)-frag.str>9 && strcmp(xmlstring(frag.str+1:frag.str+8),'![CDATA[') |
| |
| frag = tag_cdata(frag); |
| else |
| |
| if ~isempty(frag.end) |
| endmk = ['/' frag.end '>']; |
| else |
| endmk = '/>'; |
| end |
| if strcmp(xmlstring(frag.str+1:frag.str+length(frag.end)+2),endmk) || ... |
| strcmp(strip(xmlstring(frag.str+1:frag.str+length(frag.end)+2)),endmk) |
| frag.str = frag.str + length(frag.end)+3; |
| return |
| else |
| frag = tag_element(frag); |
| end |
| end |
| end |
| end |
| end |
| end |
|
|
| |
| function frag = tag_element(frag) |
| global xmlstring xtree Xparse_count; |
| close = xml_findstr(xmlstring,'>',frag.str,1); |
| if isempty(close) |
| error('[XML] Tag < opened but not closed.'); |
| else |
| empty = strcmp(xmlstring(close-1:close),'/>'); |
| if empty |
| close = close - 1; |
| end |
| starttag = normalize(xmlstring(frag.str+1:close-1)); |
| nextspace = xml_findstr(starttag,' ',1,1); |
| attribs = ''; |
| if isempty(nextspace) |
| name = starttag; |
| else |
| name = starttag(1:nextspace-1); |
| attribs = starttag(nextspace+1:end); |
| end |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = element; |
| xtree{Xparse_count}.name = strip(name); |
| if frag.parent |
| xtree{Xparse_count}.parent = frag.parent; |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| end |
| if ~isempty(attribs) |
| xtree{Xparse_count}.attributes = attribution(attribs); |
| end |
| if ~empty |
| contents = fragment; |
| contents.str = close+1; |
| contents.end = name; |
| contents.parent = Xparse_count; |
| contents = compile(contents); |
| frag.str = contents.str; |
| else |
| frag.str = close+2; |
| end |
| end |
|
|
| |
| function frag = tag_pi(frag) |
| global xmlstring xtree Xparse_count; |
| close = xml_findstr(xmlstring,'?>',frag.str,1); |
| if isempty(close) |
| warning('[XML] Tag <? opened but not closed.') |
| else |
| nextspace = xml_findstr(xmlstring,' ',frag.str,1); |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = pri; |
| if nextspace > close || nextspace == frag.str+2 |
| xtree{Xparse_count}.value = erode(xmlstring(frag.str+2:close-1)); |
| else |
| xtree{Xparse_count}.value = erode(xmlstring(nextspace+1:close-1)); |
| xtree{Xparse_count}.target = erode(xmlstring(frag.str+2:nextspace)); |
| end |
| if frag.parent |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| xtree{Xparse_count}.parent = frag.parent; |
| end |
| frag.str = close+2; |
| end |
|
|
| |
| function frag = tag_comment(frag) |
| global xmlstring xtree Xparse_count; |
| close = xml_findstr(xmlstring,'-->',frag.str,1); |
| if isempty(close) |
| warning('[XML] Tag <!-- opened but not closed.') |
| else |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = comment; |
| xtree{Xparse_count}.value = erode(xmlstring(frag.str+4:close-1)); |
| if frag.parent |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| xtree{Xparse_count}.parent = frag.parent; |
| end |
| frag.str = close+3; |
| end |
|
|
| |
| function frag = tag_cdata(frag) |
| global xmlstring xtree Xparse_count; |
| close = xml_findstr(xmlstring,']]>',frag.str,1); |
| if isempty(close) |
| warning('[XML] Tag <![CDATA[ opened but not closed.') |
| else |
| Xparse_count = Xparse_count + 1; |
| xtree{Xparse_count} = cdata; |
| xtree{Xparse_count}.value = xmlstring(frag.str+9:close-1); |
| if frag.parent |
| xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count]; |
| xtree{Xparse_count}.parent = frag.parent; |
| end |
| frag.str = close+3; |
| end |
|
|
| |
| function all = attribution(str) |
| |
| nbattr = 0; |
| all = cell(nbattr); |
| |
| while 1, |
| eq = xml_findstr(str,'=',1,1); |
| if isempty(str) || isempty(eq), return; end |
| id = sort([xml_findstr(str,'"',1,1),xml_findstr(str,'''',1,1)]); id=id(1); |
| nextid = sort([xml_findstr(str,'"',id+1,1),xml_findstr(str,'''',id+1,1)]);nextid=nextid(1); |
| nbattr = nbattr + 1; |
| all{nbattr}.key = strip(str(1:(eq-1))); |
| all{nbattr}.val = entity(str((id+1):(nextid-1))); |
| str = str((nextid+1):end); |
| end |
|
|
| |
| function elm = element |
| global Xparse_count; |
| elm = struct('type','element','name','','attributes',[],'contents',[],'parent',[],'uid',Xparse_count); |
| |
| |
| function cdat = chardata |
| global Xparse_count; |
| cdat = struct('type','chardata','value','','parent',[],'uid',Xparse_count); |
| |
| |
| function cdat = cdata |
| global Xparse_count; |
| cdat = struct('type','cdata','value','','parent',[],'uid',Xparse_count); |
| |
| |
| function proce = pri |
| global Xparse_count; |
| proce = struct('type','pi','value','','target','','parent',[],'uid',Xparse_count); |
|
|
| |
| function commt = comment |
| global Xparse_count; |
| commt = struct('type','comment','value','','parent',[],'uid',Xparse_count); |
|
|
| |
| function frg = fragment |
| frg = struct('str','','parent','','end',''); |
|
|
| |
| function str = prolog(str) |
| |
| b = 1; |
| |
| start = xml_findstr(str,'<',1,1); |
| if isempty(start) |
| error('[XML] No tag found.') |
| end |
| |
| if strcmpi(str(start:start+2),'<?x') |
| close = xml_findstr(str,'?>',1,1); |
| if ~isempty(close) |
| b = close + 2; |
| else |
| warning('[XML] Header tag incomplete.') |
| end |
| end |
| |
| start = xml_findstr(str,'<!DOCTYPE',b,1); |
| if ~isempty(start) |
| close = xml_findstr(str,'>',start+9,1); |
| if ~isempty(close) |
| b = close + 1; |
| dp = xml_findstr(str,'[',start+9,1); |
| if (~isempty(dp) && dp < b) |
| k = xml_findstr(str,']>',start+9,1); |
| if ~isempty(k) |
| b = k + 2; |
| else |
| warning('[XML] Tag [ in DOCTYPE opened but not closed.') |
| end |
| end |
| else |
| warning('[XML] Tag DOCTYPE opened but not closed.') |
| end |
| end |
| |
| str = str(b:end); |
|
|
| |
| function str = strip(str) |
| str(isspace(str)) = ''; |
|
|
| |
| function str = normalize(str) |
| |
| i = isspace(str); |
| i = find(i == 1); |
| str(i) = ' '; |
| |
| if ~isempty(i) |
| j = i - [i(2:end) i(end)]; |
| str(i(j == -1)) = []; |
| end |
|
|
| |
| function str = entity(str) |
| str = strrep(str,'<','<'); |
| str = strrep(str,'>','>'); |
| str = strrep(str,'"','"'); |
| str = strrep(str,''',''''); |
| str = strrep(str,'&','&'); |
| |
| |
| function str = erode(str) |
| if ~isempty(str) && str(1)==' ', str(1)=''; end; |
| if ~isempty(str) && str(end)==' ', str(end)=''; end; |
|
|
| |
| function k = xml_findstr(s,p,i,n) |
| |
| |
| j = strfind(s,p); |
| k = j(j>=i); |
| if ~isempty(k), k = k(1:min(n,length(k))); end |
|
|