Skip to main content
guest
Join
|
Help
|
Sign In
Liberty BASIC Programmer's Encyc
Home
guest
|
Join
|
Help
|
Sign In
Wiki Home
Recent Changes
Pages and Files
Members
Home
General Tutorials
Advanced Tutorials
GUI Programming
Graphics and Games
Strings and Text
Numbers and Math
Using Files
Windows API
Communications
Programmer's Tools
Articles by Date
FAQs
Rosetta Code
General Articles
Newsletters Contents
Table of Contents
Demos
Submit Articles
TOS and License
XML Data Storage
Edit
17
…
1
Tags
strings
edit
Save
Cancel
Notify
RSS
Backlinks
Source
Print
Export (PDF)
<img id="wikitext@@toc@@normal" class="WikiMedia WikiMediaToc" title="Table of Contents" src="/site/embedthumbnail/toc/normal?w=225&h=100"/><h1>XML for Data storage</h1> <tt>by {$creator} {$revisiondate} {$revisionid}</tt><br /> <br /> <h2>XML</h2> XML is a flexible, convenient, way of storing data, once you understand how.<br /> Programs typically evolve thru several versions. This often results in different data items being stored by different program versions. This has meant careful tracking of data version vs program versions, as programs could not correctly read data from the wrong data version.<br /> XML is flexible as names are stored with the data. Names which are understood can be read, while others are ignored. The downside is that missing variables may require defaults.<br /> XML is a very convenient to use. The rules are quite simple.<br /> XML consists of elements:<br /> <br /> <name> text </name><br /> <br /> Every opening <name> is paired with a closing </name).<br /> Names can be almost anything but are case sensitive.<br /> Elements can be wholly contained within other elements.<br /> Elements cannot overlap.<br /> <name1> <XYZ> </XYZ> </name1> legal<br /> <name1> <XYZ> </name1> </XYZ> not legal<br /> <br /> <br /> for more details visit: http://www.w3schools.com/xml/xml_syntax.asp<br /> <br /> <h2>Saving Data</h2> The following example shows only the code to open and write a file.<br /> <pre class="lb">sub FileSaveAs<br/> filedialog "Save file", FilePath$+"*.Sudoku", FullName$<br/> call GetFileName$<br/> if FileName$="" then notice "No file chosen!": end<br/> call FileSave<br/>end sub<br/><br/>sub FileSave<br/> open FullName$ for output as #File<br/> FileOpen = True<br/> #File "<XML>"<br/> #File "<!-- "; Date$();" "; Time$();" ";FullName$;" -->" 'XML comment<br/> #File " <Sudoku>"<br/> call SaveOptions<br/> call SaveCells<br/> #File " </Sudoku>"<br/> #File "</XML>"<br/> close #File<br/> FileOpen = False<br/> notice "Puzzle Saved ";chr$(13);_<br/> "File Name ";FileName$;chr$(13);_<br/> "at ";FilePath$<br/>end sub<br/><br/>sub SaveCells<br/> for row = 1 to 9: for col = 1 to 9<br/> if CellValue(row,col) <> 0 then<br/> call SaveOneCell row, col, CellValue(row,col), CellStage(row,col)<br/> end if<br/> next col: next row<br/>end sub<br/><br/>sub SaveOneCell row,col,value,stage<br/> #File " <Cell> ";<br/> #File "<Row> " ;row;<br/> #File " </Row>";<br/> #File "<Col> " ;col;<br/> #File " </Col>";<br/> #File "<Value> ";value;<br/> #File " </Value>";<br/> if stage <> 1 then<br/> #File "<Stage> ";right$(" ";str$(stage),2);<br/> #File " </Stage>";<br/> else<br/> #File " "<br/> end if<br/> #File " </Cell>"<br/>end sub<br/><br/>sub SaveOptions<br/> #File " <Size> ";9;" </Size>"<br/> #File " <Diagonal>";<br/> if DiagActive then #File " On "; else #File " Off ";<br/> #File "</Diagonal>"<br/> #File " <Possible>";<br/> if PossibleShow then #File " On "; else #File " Off ";<br/> #File "</Possible>"<br/> #File " <Reduce> ";<br/> if ReduceActive then #File " On "; else #File " Off ";<br/> #File "</Reduce>"<br/>end sub<br/><br/>sub GetFileName$<br/> for pos = len(FullName$) to 1 step -1<br/> if mid$(FullName$, pos, 1) = "\" then exit for<br/> next pos<br/> FileName$ = mid$(FullName$,pos+1)<br/> FilePath$ = left$(FullName$,pos)<br/>end sub</pre> <h2>Example data file:</h2> <pre class="xml"><XML><br/><!-- Mar 12, 2014 10:56:25 C:\Data\Sudoku\3.40.88.Sudoku --><br/> <Sudoku><br/> <Size> 9 </Size><br/> <Diagonal> Off </Diagonal><br/> <Possible> Off </Possible><br/> <Reduce> Off </Reduce><br/> <Cell> <Row> 1 </Row><Col> 1 </Col><Value> 7 </Value> </Cell><br/> <Cell> <Row> 1 </Row><Col> 7 </Col><Value> 9 </Value> </Cell><br/> <Cell> <Row> 1 </Row><Col> 8 </Col><Value> 3 </Value> </Cell><br/> <Cell> <Row> 2 </Row><Col> 4 </Col><Value> 6 </Value> </Cell><br/> <Cell> <Row> 2 </Row><Col> 5 </Col><Value> 4 </Value> </Cell><br/> <Cell> <Row> 2 </Row><Col> 6 </Col><Value> 7 </Value><Stage> 2 </Stage> </Cell><br/> <Cell> <Row> 3 </Row><Col> 1 </Col><Value> 1 </Value> </Cell><br/> <Cell> <Row> 9 </Row><Col> 2 </Col><Value> 4 </Value> </Cell><br/> <Cell> <Row> 9 </Row><Col> 3 </Col><Value> 1 </Value> </Cell><br/> </Sudoku><br/></XML></pre> <img id="wikitext@@toc@@normal" class="WikiMedia WikiMediaToc" title="Table of Contents" src="/site/embedthumbnail/toc/normal?w=225&h=100"/><h2>Reading Data</h2> Reading XML data is a little more work than writing it, but still not difficult. Since you wrote the file, you only need to be able to read what you wrote. <br /> For this code segment, variable names with initial letter capitalized are assumed to have been declared global.<br /> <pre class="lb">sub FileLoad<br/> filedialog "Load file...", FilePath$+"*.Sudoku", FullName$<br/> call GetFileName$<br/> if FileName$="" then notice "No file chosen!": end<br/> open FullName$ for input as #File<br/> FileOpen = True<br/> call ProcessFile<br/>end sub<br/><br/>sub GetFileName$<br/> for pos = len(FullName$) to 1 step -1<br/> if mid$(FullName$, pos, 1) = "\" then exit for<br/> next pos<br/> FileName$ = mid$(FullName$,pos+1)<br/> FilePath$ = left$(FullName$,pos)<br/>end sub<br/><br/>sub ProcessFile<br/> do<br/> call FindTag<br/> select case Tag$<br/> case "<XML>"<br/> case "<Sudoku>":call InitCellArrays :call LoadPuzzle<br/> case "</XML>" :close #File: FileOpen = False<br/> end select<br/> loop until FileOpen = False<br/>end sub<br/><br/>sub LoadPuzzle<br/> do<br/> call FindTag<br/> select case Tag$<br/> case "<Cell>" :call LoadCell<br/> case "<Diagonal>" :DiagActive = FindContentLogic()<br/> case "</Diagonal>"<br/> case "<Possible>" :PossibleShow = FindContentLogic()<br/> case "</Possible>"<br/> case "<Reduce>" :ReduceActive = FindContentLogic()<br/> case "</Reduce>"<br/> case "<Size>" :call CheckSize<br/> case "</Size>"<br/> case "</Sudoku>" :call ShowRefresh: exit sub<br/> end select<br/> loop until FileOpen = False<br/>end sub<br/><br/>sub LoadCell<br/> stage = 1<br/> do<br/> call FindTag<br/> select case Tag$<br/> case "<Row>" :row = FindContentValue()<br/> case "</Row>"<br/> case "<Col>" :col = FindContentValue()<br/> case "</Col>"<br/> case "<Value>" :val = FindContentValue()<br/> case "</Value>"<br/> case "<Stage>" :stage = FindContentValue()<br/> case "</Stage>"<br/> case "</Cell>" :CellValue(row,col) = val: CellStage(row,col) = stage<br/>exit sub<br/> end select<br/> loop until FileOpen = False<br/>end sub<br/><br/>sub LoadInputBuffer<br/> if eof(#File) <> 0 then close #File: FileOpen = False: exit sub<br/> do until (len(InputBuffer$) > 0 ) or (eof(#File) <> 0 ) 'skip blank lines<br/> line input #File, InputBuffer$<br/> loop<br/>end sub<br/><br/>sub FindTag<br/> if len(InputBuffer$) = 0 then call LoadInputBuffer<br/> for i = 1 to len(InputBuffer$)<br/> if mid$(InputBuffer$, i, 1) = "<" then exit for<br/> next i<br/> for j=i+1 to len(InputBuffer$)<br/> if mid$(InputBuffer$, j, 1) = ">" then exit for<br/> next j<br/> Tag$ = mid$(InputBuffer$, i, j-i+1)<br/> InputBuffer$ = mid$(InputBuffer$, j+1) 'remove tag<br/>end sub<br/><br/>function FindContentString$()<br/> for i = 1 to len(InputBuffer$)<br/> if mid$(InputBuffer$, i, 1) = "<" then exit for<br/> next i<br/> FindContentString$ = trim$(mid$(InputBuffer$, 1, i-1))<br/> InputBuffer$ = mid$(InputBuffer$, i) 'remove content<br/>end function<br/><br/>function FindContentValue()<br/> for i = 1 to len(InputBuffer$)<br/> if mid$(InputBuffer$, i, 1) = "<" then exit for<br/> next i<br/> FindContentValue = val(mid$(InputBuffer$, 1, i-1))<br/> InputBuffer$ = mid$(InputBuffer$, i) 'remove content<br/>end function<br/><br/>function FindContentLogic()<br/> FindContentLogic = False<br/> if FindContentString$() = "on" then FindContentLogic = True<br/>end function</pre> 20 Mar 2014 9:18
Javascript Required
You need to enable Javascript in your browser to edit pages.
help on how to format text
Turn off "Getting Started"
Home
...
Loading...