November 24, 2020, 07:04:57 AM

Author Topic: [bb] XML code by GrahamK [ 1+ years ago ]  (Read 624 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] XML code by GrahamK [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : XML code
Author : GrahamK
Posted : 1+ years ago

Description : Will add further comments/descriptions later

Code :
Code: BlitzBasic
  1. ; XML load / parse / save functions
  2.  
  3. Type sdXMLnodelist
  4.         Field node.sdxmlnode
  5.         Field nextnode.sdxmlnodelist
  6.         Field prevnode.sdxmlnodelist
  7. End Type
  8.  
  9. ; for internal use, do not use in code outside of this file
  10. Type sdXMLworklist
  11.         Field node.sdxmlnode
  12. End Type
  13.  
  14.  
  15. Type sdXMLnode
  16.         Field tag$,value$,path$
  17.         Field firstattr.sdXMLattr
  18.         Field lastattr.sdXMLattr       
  19.         Field attrcount,fileid
  20.         Field endtag$
  21.        
  22.         ; linkage functionality
  23.         Field firstchild.sdXMLnode
  24.         Field lastchild.sdXMLnode
  25.         Field childcount
  26.         Field nextnode.sdXMLnode
  27.         Field prevnode.sdXMLnode
  28.         Field parent.sdXMLnode
  29. End Type
  30.  
  31. Type sdXMLattr
  32.         Field name$,value$
  33.         Field sibattr.sdXMLattr
  34.         Field parent.sdxmlnode
  35. End Type
  36.  
  37. Global SDXMLFILEID
  38.  
  39. Function sdReadXML.sdXMLnode(filename$)
  40.         infile = ReadFile(filename$)
  41.         SDXMLFILEID=MilliSecs()
  42.         x.sdxmlnode = sdXMLReadNode(infile,Null)
  43.         CloseFile infile
  44.         Return x
  45. End Function
  46.  
  47. Function sdWriteXML(filename$,node.sdxmlnode,writeroot=False)
  48.         outfile = WriteFile(filename$)
  49.         WriteLine outfile,"<?xml version="+Chr$(34)+"1.0"+Chr$(34)+" ?>"
  50.         sdXMLwriteNode(outfile,node)
  51.         CloseFile outfile
  52. End Function
  53.  
  54.  
  55.  
  56. Function sdXMLOpenNode.sdxmlnode(parent.sdxmlnode,tag$="")
  57.         ;gak debuglog "Opening new node"
  58.         x.sdxmlnode = New sdxmlnode
  59.         x       ag$=tag$
  60.         xfileid = SDXMLFILEID; global indicator to group type entries (allows multiple XML files to be used)
  61.         sdXMLaddNode(parent,x)
  62.         Return x
  63. End Function
  64.  
  65. Function sdXMLCloseNode.sdxmlnode(node.sdxmlnode)
  66.         ;gak debuglog "Closing node ["+node     ag$+"]"
  67.         If nodeparent <> Null Then
  68.                 ;gak debuglog "Returning to parent ["+nodeparent        ag$+"]"
  69.         Else
  70.                 ;gak debuglog "No Parent found"
  71.         End If
  72.         Return nodeparent
  73. End Function
  74.  
  75. ; adds node to end of list (need separate function for insert, or mod this on)
  76. Function sdXMLAddNode(parent.sdxmlnode,node.sdxmlnode)
  77.         If parent <> Null
  78.                 ;gak debuglog "Parent of node = ["+parent       ag$+"]"
  79.                 If parentchildcount = 0 Then
  80.                         parentfirstchild = node
  81.                 Else
  82.                         parentlastchild
  83. extnode = node
  84.                 End If
  85.                 nodeprevnode = parentlastchild
  86.                 parentlastchild = node
  87.                 parentchildcount = parentchildcount +1
  88.                 nodepath$ = parentpath$+parent  ag$
  89.         End If
  90.         nodeparent = parent
  91.         nodepath$=nodepath$+"/"
  92.         ;gak debuglog "path to ["+node  ag$+"]={"+nodepath$+"}"
  93. End Function
  94.  
  95.  
  96. Function sdXMLDeleteNode(node.sdxmlnode)
  97.         n.sdxmlnode = nodefirstchild
  98.         ; delete any children recursively
  99.         While n <> Null
  100.                 nn.sdxmlnode= n
  101. extnode
  102.                 sdXMLdeletenode(n)
  103.                 n = nn
  104.         Wend
  105.  
  106.         ; delete attributes for this node
  107.         a.sdxmlattr = nodefirstattr
  108.         While a <> Null
  109.                 na.sdxmlattr = asibattr
  110.                 Delete a
  111.                 a = na
  112.         Wend
  113.  
  114.         ; dec parents child count
  115.         If nodeparent <> Null
  116.                 nodeparentchildcount = nodeparentchildcount -1
  117.                
  118.                 ; heal linkages
  119.                 If nodeprevnode <> Null Then nodeprevnode
  120. extnode = node
  121. extnode
  122.                 If node
  123. extnode <> Null Then node
  124. extnodeprevnode = nodeprevnode
  125.                 If nodeparentfirstchild = node Then nodeparentfirstchild = node
  126. extnode
  127.                 If nodeparentlastchild = node Then nodeparentlastchild = nodeprevnode
  128.         End If
  129.         ; delete this node             
  130. ;       ;gak debuglog "DELETING:"+node  ag$
  131.         Delete node
  132.  
  133. End Function
  134.  
  135.  
  136. ; node functions
  137.  
  138. Function sdXMLfindNode.sdXMLnode(node.sdxmlnode,path$)
  139.         ;gak debuglog "------------- Perfoming Find ("+path$+")------------"
  140.  
  141.         ret.sdXMLnode = Null
  142.         p=Instr(path$,"/")
  143.         If p > 0 Then
  144.                 tag$=Left$(path$,p-1)
  145.                 ;gak debuglog "Looking for ["+tag$+"]"
  146.                 a.sdxmlnode = node
  147.                 While ret=Null And a<>Null
  148.                         ;gak debuglog "Checking...["+a  ag$+"]"
  149.                         If Lower(tag$)=Lower(a  ag$) Then
  150.                                 If p=Len(path$) Then
  151.                                                 ;gak debuglog "Found..."
  152.                                                 ret = a
  153.                                 Else
  154.                                         If afirstchild <> Null Then
  155.                                                 ret = sdxmlfindnode(afirstchild,Mid$(path$,p+1))
  156.                                         End If
  157.                                 End If
  158.                         End If
  159.                         a = a
  160. extnode
  161.                 Wend
  162.         End If
  163.         Return ret
  164. End Function
  165.  
  166. Function sdXMLDeleteList(nl.sdxmlnodelist)
  167.         While nl <> Null
  168.                 na.sdxmlnodelist = nl
  169. extnode
  170.                 Delete nl
  171.                 nl = na
  172.         Wend
  173. End Function
  174.  
  175.  
  176. Function sdXMLSelectNodes.sdxmlnodelist(node.sdxmlnode,path$,recurse=True)
  177.         root.sdxmlnodelist=Null
  178.         sdxmlselectnodesi(node,path$,recurse)
  179.         prev.sdxmlnodelist=Null
  180.         c = 0
  181.         For wl.sdxmlworklist = Each sdxmlworklist
  182.                 c = c + 1
  183.                 nl.sdxmlnodelist = New sdxmlnodelist
  184.                 nl
  185. ode = wl
  186. ode
  187.                 If prev = Null Then
  188.                         root = nl
  189.                         prev = nl
  190.                 Else
  191.                         prev
  192. extnode = nl
  193.                         nlprevnode = prev
  194.                 End If
  195.                 prev = nl
  196.                 Delete wl
  197.         Next
  198.         ;gak debuglog "XML: "+c+" nodes selected"
  199.         Return root
  200. End Function
  201.  
  202. ; internal selection function, do not use outside this file
  203. Function sdXMLSelectNodesI(node.sdxmlnode,path$,recurse=True)
  204.         wl.sdXMLworklist=Null
  205.         ;gak debuglog "------------- Perfoming Select ("+path$+")------------"
  206.         If node = Null Then
  207.                  ;gak debuglog "Search node is null!!!"
  208.         End If
  209.         ret.sdXMLnode = Null
  210.         p=Instr(path$,"/")
  211.         If p > 0 Then
  212.                 tag$=Left$(path$,p-1)
  213.                 a.sdxmlnode = node
  214.                 While a<>Null
  215.                         ;gak debuglog "Looking for {"+path$+"} in {"+apath$+a   ag$+"/}  {"+Lower(Right$(apath$+a       ag$+"/",Len(path$)))+"} @"
  216.                         If Lower(path$)=Lower(Right$(apath$+a   ag$+"/",Len(path$))) Then
  217.                                         wl = New sdXMLworklist
  218.                                         wl
  219. ode = a
  220.                                         ;gak debuglog ">>FOUND"
  221.                         End If
  222.                         If afirstchild <> Null And (recurse) Then
  223.                                 sdXMLSelectNodesI(afirstchild,path$)
  224.                         End If
  225.                         a = a
  226. extnode
  227.                 Wend
  228.         End If
  229.  
  230. End Function
  231.  
  232. Function sdXMLNextNode.sdXMLnode(node.sdXMLnode)
  233.         Return node
  234. extnode
  235. End Function
  236.  
  237. Function sdXMLPrevNode.sdXMLnode(node.sdXMLnode)
  238.         Return nodeprevnode
  239. End Function
  240.  
  241. Function sdXMLAddAttr(node.sdxmlnode,name$,value$)
  242.         ;gak debuglog "XML:adding attribute "+name$+"="+value$+" ("+Len(value$)+")"
  243.         a.sdxmlattr = New sdxmlattr
  244.         a
  245. ame$ = name$
  246.         avalue$ = value$
  247.         If nodeattrcount = 0 Then
  248.                 nodefirstattr = a
  249.         Else
  250.                 nodelastattrsibattr = a
  251.         End If
  252.         nodelastattr=a
  253.         nodeattrcount = nodeattrcount + 1
  254.         aparent = node
  255. End Function
  256.  
  257.  
  258. Function sdXMLReadNode.sdxmlnode(infile,parent.sdXMLnode,pushed=False)
  259.         mode = 0
  260.         root.sdxmlnode = Null
  261.         cnode.sdxmlnode = Null
  262.         x.sdXMLnode = Null
  263.         ispushed = False
  264.         done = False
  265.         While (Not done) And (Not Eof(infile))
  266.                 c = ReadByte(infile)
  267.                 If c<32 Then c=32
  268.                 ch$=Chr$(c)
  269. ;               ;gak debuglog "{"+ch$+"} "+c+" mode="+mode
  270.                 Select mode
  271.                   Case 0 ; looking for the start of a tag, ignore everything else
  272.                         If ch$ = "<" Then
  273.                                 mode = 1; start collecting the tag
  274.                         End If
  275.                   Case 1 ; check first byte of tag, ? special tag
  276.                     If ch$ = "?" Or ch$ = "!" Then
  277.                                 mode = 0; class special nodes as garbage & consume
  278.                         Else
  279.                                 If ch$ = "/" Then
  280.                                         mode = 2 ; move to collecting end tag
  281.                                         xendtag$=ch$
  282.                                         ;gak debuglog "** found end tag"
  283.                                 Else
  284.                                         cnode=x
  285.                                         x.sdXMLnode = sdXMLOpennode(cnode)
  286.                                         If cnode=Null Then root=x
  287.                                         x       ag$=ch$
  288.                                         mode = 3 ; move to collecting start tag
  289.                                 End If
  290.                         End If
  291.                   Case 2 ; collect the tag name (close tag)
  292.                         If ch$=">" Then
  293.                                 mode = 0 ; end of the close tag so jump out of loop
  294.                                 ;done = True
  295.                                 x = sdXMLclosenode(x)
  296.                         Else
  297.                                 xendtag$ = xendtag$ + ch$
  298.                         End If
  299.                   Case 3 ; collect the tag name
  300.                         If ch$=" " Then
  301.                                 ;gak debuglog "TAG:"+x  ag$
  302.                                 mode = 4 ; tag name collected, move to collecting attributes
  303.                         Else
  304.                                 If ch$="/" Then
  305.                                         ;gak debuglog "TAG:"+x  ag$
  306.                                         xendtag$=x      ag$
  307.                                         mode = 2; start/end tag combined, move to close
  308.                                 Else
  309.                                         If ch$=">" Then
  310.                                                 ;gak debuglog "TAG:"+x  ag$
  311.                                                 mode = 20; tag closed, move to collecting value
  312.                                         Else
  313.                                                 x       ag$ = x ag$ + ch$
  314.                                         End If
  315.                                 End If
  316.                         End If
  317.                   Case 4 ; start to collect attributes
  318.                     If Lower(ch$)>="a" And Lower(ch$)<="z" Then
  319.                                 aname$=ch$;
  320.                             mode = 5; move to collect attribute name
  321.                         Else
  322.                                 If ch$=">" Then
  323.                                         xvalue$=""
  324.                                         mode = 20; tag closed, move to collecting value
  325.                                 Else
  326.                                         If ch$="/" Then
  327.                                                 mode = 2 ; move to collecting end tag
  328.                                                 xendtag$=ch$
  329.                                                 ;gak debuglog "** found end tag"
  330.                                         End If
  331.                                 End If
  332.                         End If
  333.                   Case 5 ; collect attribute name
  334.                     If ch$="=" Then
  335.                           ;gak debuglog "ATT:"+aname$
  336.                           aval$=""
  337.                           mode = 6; move to collect attribute value
  338.                         Else
  339.                           aname$=aname$+ch$
  340.                         End If
  341.                   Case 6 ; collect attribute value
  342.                     If c=34 Then
  343.                                 mode = 7; move to collect string value
  344.                         Else
  345.                                 If c <= 32 Then
  346.                                         ;gak debuglog "ATV:"+aname$+"="+aval$
  347.                                         sdXMLAddAttr(x,aname$,aval$)
  348.                                         mode = 4; start collecting a new attribute
  349.                                 Else
  350.                                         aval$=aval$+ch$
  351.                                 End If
  352.                         End If
  353.                   Case 7 ; collect string value
  354.                         If c=34 Then
  355.                                 ;gak debuglog "ATV:"+aname$+"="+aval$
  356.                                 sdxmlADDattr(x,aname$,aval$)
  357.                                 mode = 4; go and collect next attribute
  358.                         Else
  359.                                 aval$=aval$+ch$
  360.                         End If
  361.                   Case 20 ; COLLECT THE VALUE PORTION
  362.                         If ch$="<" Then
  363.                                 ;gak debuglog "VAL:"+x  ag$+"="+xvalue$
  364.                                 mode=1; go to tag checking
  365.                         Else
  366.                                 xvalue$=xvalue$+ch$
  367.                         End If
  368.                 End Select
  369.                
  370.                 If Eof(infile) Then done=True
  371.        
  372.         Wend
  373.  
  374.         Return root
  375.  
  376. End Function
  377.  
  378. ; write out an XML node (and children)
  379. Function sdXMLWriteNode(outfile,node.sdxmlnode,tab$="")
  380. ;       ;gak debuglog "Writing...."+node        ag$+".."
  381.         s$="<"+node     ag$
  382.         a.sdxmlattr = nodefirstattr
  383.         While a<>Null
  384. ;               ;gak debuglog "Writing attr ["+a
  385. ame$+"]=["+avalue$+"]"
  386.                 s$ = s$+" "+Lower(a
  387. ame$)+"="+Chr$(34)+avalue$+Chr$(34)
  388.                 a = asibattr
  389.         Wend
  390.        
  391.         If nodevalue$="" And nodechildcount = 0 Then
  392.                 s$=s$+"/>"
  393.                 et$=""
  394.         Else
  395.                 s$=s$+">"+nodevalue$
  396.                 et$="</"+node   ag$+">"
  397.         End If
  398.        
  399.         WriteLine outfile,sdXMLcleanStr$(tab$+s$)
  400.         n.sdxmlnode = nodefirstchild
  401.         While n <> Null
  402.                 sdXMLwriteNode(outfile,n,tab$+"  ")
  403.                 n = n
  404. extnode
  405.         Wend
  406.        
  407.         If et$<> "" Then WriteLine outfile,sdXMLcleanStr$(tab$+et$)
  408.  
  409. End Function
  410.  
  411.  
  412.  
  413.  
  414. ; remove non-visible chars from the output stream
  415. Function sdXMLCleanStr$(s$)
  416.         a$=""
  417.         For i = 1 To Len(s$)
  418.                 If Asc(Mid$(s$,i,1))>=32 Then a$ = a$ +Mid$(s$,i,1)
  419.         Next
  420.         Return a$
  421.  
  422. End Function
  423.  
  424. ; attribute functions
  425. ; return an attribute of a given name
  426. Function sdXMLFindAttr.sdXMLattr(node.sdxmlnode,name$)
  427.         ret.sdXMLattr = Null
  428.         If node <> Null Then
  429.                 a.sdxmlattr = nodefirstattr
  430.                 done = False
  431.                 While ret=Null And a<>Null
  432.                         If Lower(name$)=Lower(a
  433. ame$) Then
  434.                                 ret = a
  435.                         End If
  436.                         a = asibattr
  437.                 Wend
  438.         End If
  439.         Return ret
  440. End Function
  441.  
  442. ; return an attribute value as a string
  443. Function sdXMLAttrValueStr$(node.sdxmlnode,name$,dflt$="")
  444.         ret$=dflt$
  445.         a.sdxmlattr = sdXMLfindattr(node,name$)
  446.         If a <> Null Then ret$=avalue$
  447.         Return ret$
  448. End Function
  449.  
  450. ; return an attribute value as an integer
  451. Function sdXMLAttrValueInt(node.sdxmlnode,name$,dflt=0)
  452.         ret=dflt
  453.         a.sdxmlattr = sdXMLfindattr(node,name$)
  454.         If a <> Null Then ret=avalue
  455.         Return ret
  456. End Function
  457.  
  458. ; return an attribute value as a float
  459. Function sdXMLAttrValueFloat#(node.sdxmlnode,name$,dflt#=0)
  460.         ret#=dflt#
  461.         a.sdxmlattr = sdXMLfindattr(node,name$)
  462.         If a <> Null Then ret#=avalue
  463.         Return ret
  464. End Function
  465.  
  466. ;x.sdxmlnode = sdReadXML("test.xml")
  467. ;sdwritexml("test2.xml",x)
  468.  
  469. ;f.sdxmlnode = sdxmlfindnode(x,"BB3D/NODE/MESH/")
  470. ;If f <> Null Then
  471. ;       ;gak debuglog "FOUND!!!"
  472. ;       sdxmldeletenode(f)     
  473. ;End If
  474.  
  475. ;sdwritexml("test3.xml",x)
  476.  
  477. ;nl.sdxmlnodelist = sdxmlselectnodes(x,"/VERTEX/POS/")
  478. ;While nl <> Null;
  479. ;       ;gak debuglog "Found....."+nl
  480. ode     ag$
  481. ;       nl=nl
  482. extnode
  483. ;Wend
  484. ;sdxmldeleteList(nl);
  485.  
  486.  
  487.  
  488. ;sdxmldeletenode(x)


Comments :


Pedro(Posted 1+ years ago)

 thank you Blitztastic for this code : really excellent and performant.below an upgrade to use it only in read only mode.; ID: 361; Author: Blitztastic; Date: 2002-07-05 08:57:18; Title: XML code; Updated by : Philippe C; Date: 2004-07-25 ; Description: XML loading/parsing; XML load / parse functions ;; updates done:;   =>    remove Functions And Datas which are not used to load And parse. ;   =>    Add control during the read of the file in order to get syntax errors : ;      rough controls are done;   => add 2 new functions in order to simplify the access;      pXMLGetBranch : get a branch of the xml file for a given value;      pXMLGetValue : get the value of the attributes contains in the branch;      May be is not very pure XML parsing but it is convenient to use.;; WARNING : ;   IN CASE OF WRONG RESULT OPEN FIRST THE FILE WITH IE OR MOZILLA AND CHECK IF THE ;   XML FILE IS WITHOUT ERROR.;    WHILE YOU HAVE A WRONG RESULT WITH IE OR MOZILLA THIS LIBRARY WILL RETURN AN ;   ERROR MESSAGE OR A WRONG RESULT.;    NO CONTROL ARE DONE ON THE SYNTAX : FOR THIS LIBRARY THE XML FILE IS PERFECT !;   A ROUGH CONTROL HAS BEEN ADDED, BUT IT IS NOT POSSIBLE TO KNOW WHERE THE ERROR(s) COME FROM.;;_____________________________________________________________________________________________________; TYPE;_____________________________________________________________________________________________________Type sdXMLnodelist   Field node.sdxmlnode   Field nextnode.sdxmlnodelist   Field prevnode.sdxmlnodelistEnd Type; for internal use, do not use in code outside of this fileType sdXMLworklist   Field node.sdxmlnodeEnd TypeType sdXMLnode   Field tag$,value$,path$   Field fileid      ; linkage functionality   Field firstchild.sdXMLnode   Field lastchild.sdXMLnode   Field childcount   Field nextnode.sdXMLnode   Field parent.sdXMLnodeEnd TypeGlobal SDXMLFILEID;_____________________________________________________________________________________________________; FUNCTION;_____________________________________________________________________________________________________Function sdReadXML.sdXMLnode(filename$)   infile = ReadFile(filename$)   SDXMLFILEID=MilliSecs()   x.sdxmlnode = sdXMLReadNode(infile,Null)   CloseFile infile   Return xEnd Function; adds node to end of list (need separate function for insert, or mod this on)Function sdXMLAddNode(parent.sdxmlnode,node.sdxmlnode)   If parent <> Null      ;gak debuglog "Parent of node = ["+parent   ag$+"]"      If parentchildcount = 0 Then         parentfirstchild = node      Else         parentlastchild
extnode = node      End If      parentlastchild = node      parentchildcount = parentchildcount +1      nodepath$ = parentpath$+parent   ag$   End If   nodeparent = parent   nodepath$=nodepath$+"/"   ;gak debuglog "path to ["+node   ag$+"]={"+nodepath$+"}"End FunctionFunction sdXMLSelectNodes.sdxmlnodelist(node.sdxmlnode,path$,recurse=True)   root.sdxmlnodelist=Null   sdxmlselectnodesi(node,path$,recurse)   prev.sdxmlnodelist=Null      c = 0   For wl.sdxmlworklist = Each sdxmlworklist      c = c + 1      nl.sdxmlnodelist = New sdxmlnodelist      nl
ode = wl
ode      If prev = Null Then          root = nl         prev = nl      Else         prev
extnode = nl         nlprevnode = prev      End If      prev = nl      Delete wl    Next   ;gak debuglog "XML: "+c+" nodes selected"   Return rootEnd Function; internal selection function, do not use outside this fileFunction sdXMLSelectNodesI(node.sdxmlnode,path$,recurse=True)   workList.sdXMLworklist=Null   ;gak debuglog "------------- Perfoming Select ("+path$+")------------"   If node = Null Then       ;gak debuglog "Search node is null!!!"   End If   ret.sdXMLnode = Null   p=Instr(path$,"/")   If p > 0 Then       tag$=Left$(path$,p-1)      a.sdxmlnode = node      While a<>Null          ;gak debuglog "Looking for {"+path$+"} in {"+apath$+a   ag$+"/}  {"+Lower(Right$(apath$+a   ag$+"/",Len(path$)))+"} @"         If Lower(path$)=Lower(Right$(apath$+a   ag$+"/",Len(path$))) Then               workList = New sdXMLworklist               workList
ode = a               ;gak debuglog ">>FOUND"         End If         If afirstchild <> Null And (recurse) Then            sdXMLSelectNodesI(afirstchild,path$)         End If         a = a
extnode      Wend   End IfEnd FunctionFunction sdXMLReadNode.sdxmlnode(infile,parent.sdXMLnode,pushed=False)   mode = 0   root.sdxmlnode = Null   cnode.sdxmlnode = Null   x.sdXMLnode = Null   ispushed = False   done = False   errorLT = 0      ; philippe c   errorGT = 0      ; philippe c   errorSL = 0      ; philippe c   While (Not done) And (Not Eof(infile))      c = ReadByte(infile)            If c<32 Then c=32      ch$=Chr$(c);      ;gak debuglog "{"+ch$+"} "+c+" mode="+mode      Select mode        Case 0 ; looking for the start of a tag, ignore everything else         If ch$ = "<" Then             errorLT= errorLT + 1 ; philippe C            mode = 1; start collecting the tag         End If        Case 1 ; check first byte of tag, ? special tag          If ch$ = "?" Or ch$ = "!" Then             mode = 0; class special nodes as garbage & consume         Else            If ch$ = "/" Then                errorSL = errorSL + 1 ; philippe C               mode = 2 ; move to collecting end tag               ;xendtag$=ch$               ;gak debuglog "** found end tag"            Else               cnode=x                              x.sdxmlnode = New sdxmlnode               xfileid = SDXMLFILEID; global indicator to group type entries (allows multiple XML files to be used)               sdXMLaddNode(cnode,x)                           If cnode=Null Then root=x               x   ag$=ch$               mode = 3 ; move to collecting start tag            End If         End If        Case 2 ; collect the tag name (close tag)         If ch$=">" Then             errorGT= errorGT + 1 ; philippe C            mode = 0 ; end of the close tag so jump out of loop            If x = Null Then RuntimeError "error parsing : open the file with IE or MOZILLA to check you xml file and found the error"             tmptagPHC$ = x   ag            x = xparent         End If        Case 3 ; collect the tag name          If ch$=" " Then             ;gak debuglog "TAG:"+x   ag$            mode = 4 ; tag name collected, move to collecting attributes         Else             If ch$="/" Then                errorSL = errorSL + 1 ; philippe C               ;gak debuglog "TAG:"+x   ag$               mode = 2; start/end tag combined, move to close            Else               If ch$=">" Then                  errorGT= errorGT + 1 ; philippe C                  ;gak debuglog "TAG:"+x   ag$                  mode = 20; tag closed, move to collecting value               Else                  x   ag$ = x   ag$ + ch$               End If            End If         End If        Case 4 ; start to collect attributes          If Lower(ch$)>="a" And Lower(ch$)<="z" Then             aname$=ch$;             mode = 5; move to collect attribute name         Else            If ch$=">" Then               errorGT= errorGT + 1 ; philippe C               xvalue$=""               mode = 20; tag closed, move to collecting value            Else               If ch$="/" Then                   errorSL = errorSL + 1 ; philippe C                  mode = 2 ; move to collecting end tag                  ;gak debuglog "** found end tag"               End If            End If         End If        Case 5 ; collect attribute name          If ch$="=" Then           ;gak debuglog "ATT:"+aname$           aval$=""           mode = 6; move to collect attribute value         Else           aname$=aname$+ch$         End If        Case 6 ; collect attribute value          If c=34 Then            mode = 7; move to collect string value         Else            If c <= 32 Then                ;gak debuglog "ATV:"+aname$+"="+aval$               mode = 4; start collecting a new attribute            Else                  aval$=aval$+ch$            End If         End If        Case 7 ; collect string value         If c=34 Then            ;gak debuglog "ATV:"+aname$+"="+aval$            mode = 4; go and collect next attribute         Else            aval$=aval$+ch$         End If        Case 20 ; COLLECT THE VALUE PORTION         If ch$="<" Then             errorLT = errorLT + 1 ; philippe C            ;gak debuglog "VAL:"+x   ag$+"="+xvalue$            mode=1; go to tag checking         Else            xvalue$=xvalue$+ch$         End If      End Select            If Eof(infile) Then done=True      Wend      ; added by philippe C   ; control : the amount of > is equal to the amount of <      If errorLT  <>  errorGT  Then       RuntimeError "error parsing missing '< or >' : open the file with IE or MOZILLA to check the xml file and found the error(s)"   EndIf      ; control : the amount of > has to be always even      If errorLT  <> Int(errorLT/ 2) * 2 Then       RuntimeError "error parsing missing ' tag>' : open the file with IE or MOZILLA to check the xml file and found the error(s)"   EndIf      ; control : the amount of > has to be always even      If errorGT  <> Int(errorGT/ 2) * 2 Then       RuntimeError "error parsing missing '<tag ' : open the file with IE or MOZILLA to check the xml file and found the error(s)"   EndIf   ; control : the amount of / is equal to the amount of <tag> divide by 2      If  errorGT/2 <> errorSL Then       RuntimeError "error parsing missing '/' in a tag : open the file with IE or MOZILLA to check the xml file and found the error(s)"   EndIf   Return rootEnd Function;--------------------------------------------------------------------------------; functions added par Philippe c;--------------------------------------------------------------------------------Function pXmlGetBranch.sdxmlnodeList(node.sdxmlnode,path$,value$); this function return the complete xml file branch for a path and given value; e.g : path="game/players/player/uid/",value="2"   branch.sdxmlnodelist = Null      branch.sdxmlnodelist = sdxmlselectnodes(node,path)   While branch <> Null      If branch
odevalue = value         Return branch      EndIf      branch=branch
extnode   Wend      Return branch   End FunctionFunction pXmlGetValue$(node.sdxmlnode,pathValue$); this function return the value in alhpanumeric value associate to a complete path in a branch; this function has to be used with : sdXMLselectnodesValue   path.sdxmlnodelist = Null      path.sdxmlnodelist = sdxmlselectnodes(node,pathValue)      If path <> Null Then        Return path
odevalue$   Else      Return ""   EndIfEnd Function;--------------------------------------------------------------------------------; test if the xml file is ok and attributes ok. Performance test ;--------------------------------------------------------------------------------Function test(file$=""); change the value between bracket"   If Len(file) = 0 Then file = testCreateFile()      Delay 1000 ; OS close the file   timet = MilliSecs()   timeTotal = timet   x.sdxmlnode = sdReadXML(file)   time1 = MilliSecs()-timet   timet = MilliSecs()      nl.sdxmlnodelist = pXmlGetBranch(x,"game/players/player/uid/","2")   time2 = MilliSecs()-timet   timet = MilliSecs()      v1$ = pXmlGetValue(nl
ode,"game/players/player/path/")   time3 = MilliSecs()-timet   timet = MilliSecs()      v2$ = pXmlGetValue(nl
ode,"game/players/player/follow/")   time4 = MilliSecs()-timet   timet = MilliSecs()         nl = Null   time5 = MilliSecs()-timet   timet = MilliSecs()         Delete nl   time6 = MilliSecs()-timet   timet = MilliSecs()         Delete x   time7 = MilliSecs()-timet   timet = MilliSecs()      timetotal = timet - timetotal    Print "--------------"   Print "time1 = " + time1 + " ms   read the xml file"   Print "time2 = " + time2 + " ms   search a branch"   Print "time3 = " + time3 + " ms   search an attribute"   Print "time4 = " + time4 + " ms   search an attribute"   Print "time5 = " + time5 + " ms   put null into type"   Print "time6 = " + time6 + " ms   delete type"   Print "time7 = " + time7 + " ms   delete type"   Print "        ----"   Print "TOTAL = " + timeTotal + " ms"   Print "--------------"   Print " "   Print "seached value :"   Print "v1 = " + v1$   Print "v2 = " + v2$   WaitKey   End FunctionFunction testCreateFile$(file$ = "test.xml")   If FileType(file) <> 0 Then DeleteFile file   fileOut = WriteFile(file)   Restore startdata   Read number    For T = 1 To number       Read ligne$       WriteLine fileout,ligne$       Next    CloseFile fileout   ligne$ = "result stored in : " + CurrentDir() + file      For i = 1 To Len(ligne) Step 40      Print Mid$(ligne,i,40)   Next   Print    Return file$End Function;_____________________________________________________________________________________________________; to comment when the library is used in a program or uncomment to test xml filetest() ; xml file .startdataData 31Data "<game>"Data "   <players>"Data "      <player>"Data "         <uid>1</uid>"Data "         <mouseAuthorised>1</mouseAuthorised>"Data "         <keyboardAuthorised>1</keyboardAuthorised>"Data "         <automatic>0</automatic>"Data "         <path>PATH1</path>"Data "         <follow>NO</follow>"Data "         <playerType>1</playerType>"Data "      </player>"Data "      <player>"Data "         <uid>2</uid>"Data "         <mouseAuthorised>1</mouseAuthorised>"Data "         <keyboardAuthorised>1</keyboardAuthorised>"Data "         <automatic>0</automatic>"Data "         <path>PATH2</path>"Data "         <follow>YES</follow>"Data "         <playerType>1</playerType>"Data "      </player>"Data "      <player>"Data "         <uid>3</uid>"Data "         <mouseAuthorised>1</mouseAuthorised>"Data "         <keyboardAuthorised>1</keyboardAuthorised>"Data "         <automatic>0</automatic>"Data "         <path>PATH3</path>"Data "         <follow>NO</follow>"Data "         <playerType>1</playerType>"Data "      </player>"Data "   </players>"Data "</game>";_____________________________________________________________________________________________________


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal