Notebook with self test script for xml_io_tools Package

Created as part of package testing.

Contents

Test reading/writing data structures without attributes

Create data structure without attributes

tree=[];
tree.Double = -3.14e2;
tree.Complex = 1.23+i*3.21;
tree.Empty  = [];
tree.RowVec = -2:0.8:3;
tree.ColVec = [6;7;8];
tree.Matrix = [1 2 3; 4 5 6; 7 8 9];
tree.Sparse = sparse(2,3,-5);
tree.Character = 'Q';
tree.String = 'Single String';
tree.StringCellArray = {'Jack', ' Jill ', '', 'XML Chars: <&> ', 'Special Chars: !$^*()_+=-@#{}[];:,./\?''"'};
tree.CharArray = [' Jack'; 'Jill '; '<&''">'];
tree.Boolean = (1==2);
tree.Struct.Parent.Child1 = 1;
tree.Struct.Parent.Child2 = 2;
tree.EmptyStruct = struct([]);
tree.StructArray(1).CellArray = {'John', 3.14, 0:0.11:1, [], '', {'cell', 1, 'of cells'}};
tree.StructArray(2).CellArray = {1:5, 'Jack', sparse(eye(3)), 1+sqrt(-(5:7)), {}};
fprintf('Original Structure:\n');
gen_object_display(tree) % function by Ohad Gal with some minor corrections
Original Structure:
             Double: [-314]
            Complex: [1.23+3.21i]
              Empty: [0x0 double]
             RowVec: [-2        -1.2        -0.4         0.4         1.2           2         2.8]
             ColVec: [3x1 double]
             Matrix: [3x3 double]
             Sparse: [2x3 double]
          Character: 'Q'
             String: 'Single String'
    StringCellArray: [1x5 cell] = 
                     Jack
                      Jill 
                                          XML Chars: <&> 
                     Special Chars: !$^*()_+=-@#{}[];:,./\?'"
          CharArray: ' JackJill <&'">'
            Boolean: false
             Struct: [1x1 struct]
                     Parent: [1x1 struct]
                             Child1: [1]
                             Child2: [2]
        EmptyStruct: [0x0 struct]
        StructArray: [1x2 struct]
                     CellArray: [1x6 cell] = 
                                John
                                    3.1400
                                         0    0.1100    0.2200    0.3300    0.4400    0.5500    0.6600    0.7700    0.8800    0.9900
                                                                                                    'cell'    [1]    'of cells'

                     CellArray: [1x5 cell] = 
                                     1     2     3     4     5
                                Jack
                                   (1,1)        1
   (2,2)        1
   (3,3)        1
                                   1.0000 + 2.2361i   1.0000 + 2.4495i   1.0000 + 2.6458i
                                

Write it to the XML file and display the file

xml_write('test1.xml', tree, 'test');
fprintf('File Produced:\n');
disp(textread('test1.xml','%s','delimiter','\n','whitespace',''))
File Produced:
    '<?xml version="1.0" encoding="utf-8"?>'
    '<test>'
    '   <Double>-314</Double>'
    '   <Complex>1.23+3.21i</Complex>'
    '   <Empty/>'
    '   <RowVec>-2 -1.2 -0.4 0.4 1.2 2 2.8</RowVec>'
    '   <ColVec>6 7 8</ColVec>'
    '   <Matrix>[1 2 3; 4 5 6; 7 8 9]</Matrix>'
    '   <Sparse>[0 0 0; 0 0 -5]</Sparse>'
    '   <Character>Q</Character>'
    '   <String>Single String</String>'
    '   <StringCellArray>'
    '      <item>Jack</item>'
    '      <item>Jill</item>'
    '      <item/>'
    '      <item>XML Chars: &lt;&amp;&gt;</item>'
    '      <item>Special Chars: !$^*()_+=-@#{}[];:,./\?'"</item>'
    '   </StringCellArray>'
    '   <CharArray>'
    '      <item>Jack</item>'
    '      <item>Jill</item>'
    '      <item>&lt;&amp;'"&gt;</item>'
    '   </CharArray>'
    '   <Boolean>0</Boolean>'
    '   <Struct>'
    '      <Parent>'
    '         <Child1>1</Child1>'
    '         <Child2>2</Child2>'
    '      </Parent>'
    '   </Struct>'
    '   <StructArray>'
    '      <item>'
    '         <CellArray>'
    '            <item>John</item>'
    '            <item>3.14</item>'
    '            <item>0 0.11 0.22 0.33 0.44 0.55 0.66 0.77 0.88 0.99</item>'
    '            <item/>'
    '            <item/>'
    '            <item>'
    '               <item>cell</item>'
    '               <item>1</item>'
    '               <item>of cells</item>'
    '            </item>'
    '         </CellArray>'
    '      </item>'
    '      <item>'
    '         <CellArray>'
    '            <item>1 2 3 4 5</item>'
    '            <item>Jack</item>'
    '            <item>[1 0 0; 0 1 0; 0 0 1]</item>'
    '            <item>1+2.2361i 1+2.4495i 1+2.6458i</item>'
    '            <item/>'
    '         </CellArray>'
    '      </item>'
    '   </StructArray>'
    '</test>'
tree2 = xml_read ('test1.xml');
xml_write('test2.xml', tree2, 'test');
t1=textread('test1.xml','%s');
t2=textread('test2.xml','%s');
if (length(t1)==length(t2) && all(strcmp(t1,t2)))
  fprintf('Files test1.xml and test2.xml are identical\n');
else
  fprintf('Files test1.xml and test2.xml are different\n');
  return
end
Files test1.xml and test2.xml are identical

Test reading/writing data structures with attributes

Create data structure with attributes

tree=[];
tree.Double.CONTENT = -3.14e2;
tree.Double.ATTRIBUTE.Unit = 'cm';
tree.Complex.CONTENT = 1.23+i*3.21;
tree.Complex.ATTRIBUTE.Comment = 'Complex Number';
tree.Empty.ATTRIBUTE.Comment = 'Empty';
tree.Array.CONTENT  = -2:0.8:3;
tree.Array.ATTRIBUTE.Length  = length(tree.Array.CONTENT);
tree.Array.ATTRIBUTE.Comment = 'Array of Doubles';
tree.Matrix.CONTENT = [1 2 3; 4 5 6; 7 8 9];
tree.Matrix.ATTRIBUTE.Size  = size(tree.Matrix.CONTENT);
tree.Sparse.CONTENT = sparse(2,3,-5);
tree.Sparse.ATTRIBUTE.Size  = size(tree.Sparse.CONTENT);
tree.Character.CONTENT = 'Q';
tree.Character.ATTRIBUTE.Comment = 'Single Character';
tree.String.CONTENT = 'Clothes make the man. Naked people have little or no influence on society';
tree.String.ATTRIBUTE.QuoteBy = 'Mark Twain';
tree.StringCellArray.CONTENT = {'Jack', ' Jill ', '', 'XML Chars: <&> ', 'Special Chars: !$^*()_+=-@#{}[];:,./\?''"'};
tree.StringCellArray.ATTRIBUTE.foo = 'foo';
tree.CharArray.CONTENT = [' Jack'; 'Jill '; '<&''">'];
tree.CharArray.ATTRIBUTE.foo = 'foo';
tree.Boolean.CONTENT = (1==2);
tree.Boolean.ATTRIBUTE.means = 'false';
tree.Struct.Parent.Child1 = 1;
tree.Struct.Parent.Child2.CONTENT = 2;
tree.Struct.Parent.Child2.ATTRIBUTE.foo = 'foo';
tree.Struct.Parent.ATTRIBUTE.foo = 'foo';
tree.Struct.ATTRIBUTE.foo = 'foo';
tree.EmptyStruct.CONTENT = struct([]);
tree.EmptyStruct.ATTRIBUTE.foo = 'foo';
tree.StructArray(1).CellArray.CONTENT = {'John', 3.14, 0:0.11:1, [], '', {'cell', 1, 'of cells'}};
tree.StructArray(2).CellArray = {1:5, 'Jack', sparse(eye(3)), 1+sqrt(-(5:7)), {}};
fprintf('Original Structure:\n');
gen_object_display(tree) % function by Ohad Gal with some minor corrections
Original Structure:
             Double: [1x1 struct]
                       CONTENT: [-314]
                     ATTRIBUTE: [1x1 struct]
                                Unit: 'cm'
            Complex: [1x1 struct]
                       CONTENT: [1.23+3.21i]
                     ATTRIBUTE: [1x1 struct]
                                Comment: 'Complex Number'
              Empty: [1x1 struct]
                     ATTRIBUTE: [1x1 struct]
                                Comment: 'Empty'
              Array: [1x1 struct]
                       CONTENT: [-2        -1.2        -0.4         0.4         1.2           2         2.8]
                     ATTRIBUTE: [1x1 struct]
                                 Length: [7]
                                Comment: 'Array of Doubles'
             Matrix: [1x1 struct]
                       CONTENT: [3x3 double]
                     ATTRIBUTE: [1x1 struct]
                                Size: [3  3]
             Sparse: [1x1 struct]
                       CONTENT: [2x3 double]
                     ATTRIBUTE: [1x1 struct]
                                Size: [2  3]
          Character: [1x1 struct]
                       CONTENT: 'Q'
                     ATTRIBUTE: [1x1 struct]
                                Comment: 'Single Character'
             String: [1x1 struct]
                       CONTENT: 'Clothes make the man. Naked people have little or no influence on society'
                     ATTRIBUTE: [1x1 struct]
                                QuoteBy: 'Mark Twain'
    StringCellArray: [1x1 struct]
                       CONTENT: [1x5 cell] = 
                                Jack
                                 Jill 
                                                                XML Chars: <&> 
                                Special Chars: !$^*()_+=-@#{}[];:,./\?'"
                     ATTRIBUTE: [1x1 struct]
                                foo: 'foo'
          CharArray: [1x1 struct]
                       CONTENT: ' JackJill <&'">'
                     ATTRIBUTE: [1x1 struct]
                                foo: 'foo'
            Boolean: [1x1 struct]
                       CONTENT: false
                     ATTRIBUTE: [1x1 struct]
                                means: 'false'
             Struct: [1x1 struct]
                        Parent: [1x1 struct]
                                   Child1: [1]
                                   Child2: [1x1 struct]
                                             CONTENT: [2]
                                           ATTRIBUTE: [1x1 struct]
                                                      foo: 'foo'
                                ATTRIBUTE: [1x1 struct]
                                           foo: 'foo'
                     ATTRIBUTE: [1x1 struct]
                                foo: 'foo'
        EmptyStruct: [1x1 struct]
                       CONTENT: [0x0 struct]
                     ATTRIBUTE: [1x1 struct]
                                foo: 'foo'
        StructArray: [1x2 struct]
                     CellArray: [1x1 struct]
                                CONTENT: [1x6 cell] = 
                                         John
                                             3.1400
                                                  0    0.1100    0.2200    0.3300    0.4400    0.5500    0.6600    0.7700    0.8800    0.9900
                                                                                                                               'cell'    [1]    'of cells'

                     CellArray: [1x5 cell] = 
                                     1     2     3     4     5
                                Jack
                                   (1,1)        1
   (2,2)        1
   (3,3)        1
                                   1.0000 + 2.2361i   1.0000 + 2.4495i   1.0000 + 2.6458i
                                

Write it to the XML file and display the file

xml_write('test1.xml', tree, 'test');
fprintf('File Produced:\n');
disp(textread('test1.xml','%s','delimiter','\n','whitespace',''))
File Produced:
    '<?xml version="1.0" encoding="utf-8"?>'
    '<test>'
    '   <Double Unit="cm">-314</Double>'
    '   <Complex Comment="Complex Number">1.23+3.21i</Complex>'
    '   <Empty Comment="Empty"/>'
    [1x82  char]
    '   <Matrix Size="3 3">[1 2 3; 4 5 6; 7 8 9]</Matrix>'
    '   <Sparse Size="2 3">[0 0 0; 0 0 -5]</Sparse>'
    '   <Character Comment="Single Character">Q</Character>'
    [1x114 char]
    '   <StringCellArray foo="foo">'
    '      <item>Jack</item>'
    '      <item>Jill</item>'
    '      <item/>'
    '      <item>XML Chars: &lt;&amp;&gt;</item>'
    '      <item>Special Chars: !$^*()_+=-@#{}[];:,./\?'"</item>'
    '   </StringCellArray>'
    '   <CharArray foo="foo">'
    '      <item>Jack</item>'
    '      <item>Jill</item>'
    '      <item>&lt;&amp;'"&gt;</item>'
    '   </CharArray>'
    '   <Boolean means="false">0</Boolean>'
    '   <Struct foo="foo">'
    '      <Parent foo="foo">'
    '         <Child1>1</Child1>'
    '         <Child2 foo="foo">2</Child2>'
    '      </Parent>'
    '   </Struct>'
    '   <EmptyStruct foo="foo"/>'
    '   <StructArray>'
    '      <item>'
    '         <CellArray>'
    '            <item>John</item>'
    '            <item>3.14</item>'
    '            <item>0 0.11 0.22 0.33 0.44 0.55 0.66 0.77 0.88 0.99</item>'
    '            <item/>'
    '            <item/>'
    '            <item>'
    '               <item>cell</item>'
    '               <item>1</item>'
    '               <item>of cells</item>'
    '            </item>'
    '         </CellArray>'
    '      </item>'
    '      <item>'
    '         <CellArray>'
    '            <item>1 2 3 4 5</item>'
    '            <item>Jack</item>'
    '            <item>[1 0 0; 0 1 0; 0 0 1]</item>'
    '            <item>1+2.2361i 1+2.4495i 1+2.6458i</item>'
    '            <item/>'
    '         </CellArray>'
    '      </item>'
    '   </StructArray>'
    '</test>'
tree2=xml_read ('test1.xml');
xml_write('test2.xml', tree2, 'test');
t1=textread('test1.xml','%s');
t2=textread('test2.xml','%s');
if (length(t1)==length(t2) && all(strcmp(t1,t2)))
  fprintf('Files test1.xml and test2.xml are identical\n');
else
  fprintf('Files test1.xml and test2.xml are different\n');
  return
end
Files test1.xml and test2.xml are identical

Another test of reading/writing data structures with attributes

Create data struct

tree = [];
tree.StructArray(1).ATTRIBUTE.Num = 1;
tree.StructArray(1).CellArray.CONTENT = {'John', 3.14};
tree.StructArray(1).CellArray.ATTRIBUTE.foo = 'foo';
tree.StructArray(2).CellArray = {1:5, 'Jack'};
tree.StructArray(2).ATTRIBUTE.Num = 2;
tree.StructArray(3).CONTENT = {'Jim', 123};
tree.StructArray(3).ATTRIBUTE.Num = 3;
fprintf('Original Structure:\n');
gen_object_display(tree) % function by Ohad Gal with some minor corrections
Original Structure:
    StructArray: [1x3 struct]
                 ATTRIBUTE: [1x1 struct]
                            Num: [1]
                 CellArray: [1x1 struct]
                              CONTENT: [1x2 cell] = 
                                       John
                                           3.1400
                            ATTRIBUTE: [1x1 struct]
                                       foo: 'foo'
                   CONTENT: [0x0 double]

                 ATTRIBUTE: [1x1 struct]
                            Num: [2]
                 CellArray: [1x2 cell] = 
                                 1     2     3     4     5
                            Jack
                   CONTENT: [0x0 double]

                 ATTRIBUTE: [1x1 struct]
                            Num: [3]
                 CellArray: [0x0 double]
                   CONTENT: [1x2 cell] = 
                            Jim
                               123

Write it to the XML file and display the file

xml_write('test1.xml', tree, 'test');
fprintf('File Produced:\n');
disp(textread('test1.xml','%s','delimiter','\n','whitespace',''))
File Produced:
    '<?xml version="1.0" encoding="utf-8"?>'
    '<test>'
    '   <StructArray Num="1">'
    '      <CellArray foo="foo">'
    '         <item>John</item>'
    '         <item>3.14</item>'
    '      </CellArray>'
    '   </StructArray>'
    '   <StructArray Num="2">'
    '      <CellArray>'
    '         <item>1 2 3 4 5</item>'
    '         <item>Jack</item>'
    '      </CellArray>'
    '   </StructArray>'
    '   <StructArray Num="3">'
    '      <CellArray/>'
    '      <item>Jim</item>'
    '      <item>123</item>'
    '   </StructArray>'
    '</test>'
tree2 = xml_read ('test1.xml');
xml_write('test2.xml', tree2, 'test');
t1=textread('test1.xml','%s');
t2=textread('test2.xml','%s');
if (length(t1)==length(t2) && all(strcmp(t1,t2)))
  fprintf('Files test1.xml and test2.xml are identical\n');
else
  fprintf('Files test1.xml and test2.xml are different\n');
  return
end
Files test1.xml and test2.xml are identical

Show use of ItemName preference parameter in xml_read

First use default iterator 'item'

xmlfile = fullfile(matlabroot, 'toolbox/matlab/general/info.xml');
tree = xml_read(xmlfile);
fprintf('tree.list: [%i struct]\n', length(tree.list));
gen_object_display(tree.list)
tree.list: [1 struct]
    listitem: [10x1 struct]
                                  label: 'Import Wizard'
                               callback: 'uiimport'
                                   icon: '$toolbox/matlab/icons/figureicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Profiler'
                               callback: 'profile viewer'
                                   icon: '$toolbox/matlab/icons/profiler.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'GUIDE (GUI Builder)'
                               callback: 'guide'
                                   icon: '$toolbox/matlab/icons/guideicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Notebook'
                               callback: 'notebook'
                                   icon: '$toolbox/matlab/icons/figureicon.gif'
              unsupported_DASH_platform: 'unix'

                                  label: 'Plot Tools'
                               callback: 'figure; plottools'
                                   icon: '$toolbox/matlab/icons/figureicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Time Series Tools'
                               callback: 'tstool'
                                   icon: '$toolbox/matlab/icons/figureicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Help'
                               callback: 'doc matlab/'
                                   icon: '$toolbox/matlab/icons/book_mat.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Demos'
                               callback: 'demo matlab'
                                   icon: '$toolbox/matlab/icons/demoicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'MATLAB Central (Web)'
                               callback: 'web http://www.mathworks.com/matlabcentral_redirect -browser;'
                                   icon: '$toolbox/matlab/icons/webicon.gif'
              unsupported_DASH_platform: [0x0 double]

                                  label: 'Product Page (Web)'
                               callback: 'web http://www.mathworks.com/products/matlab/ -browser;'
                                   icon: '$toolbox/matlab/icons/webicon.gif'
              unsupported_DASH_platform: [0x0 double]

Then, use ItemName = 'listitem' which matches iterator used in the file

Pref = [];
Pref.ItemName = 'listitem';
tree = xml_read(xmlfile, Pref);
fprintf('tree.list: [%i struct]\n', length(tree.list));
gen_object_display(tree.list)
tree.list: [10 struct]
                        label: 'Import Wizard'
                     callback: 'uiimport'
                         icon: '$toolbox/matlab/icons/figureicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Profiler'
                     callback: 'profile viewer'
                         icon: '$toolbox/matlab/icons/profiler.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'GUIDE (GUI Builder)'
                     callback: 'guide'
                         icon: '$toolbox/matlab/icons/guideicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Notebook'
                     callback: 'notebook'
                         icon: '$toolbox/matlab/icons/figureicon.gif'
    unsupported_DASH_platform: 'unix'

                        label: 'Plot Tools'
                     callback: 'figure; plottools'
                         icon: '$toolbox/matlab/icons/figureicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Time Series Tools'
                     callback: 'tstool'
                         icon: '$toolbox/matlab/icons/figureicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Help'
                     callback: 'doc matlab/'
                         icon: '$toolbox/matlab/icons/book_mat.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Demos'
                     callback: 'demo matlab'
                         icon: '$toolbox/matlab/icons/demoicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'MATLAB Central (Web)'
                     callback: 'web http://www.mathworks.com/matlabcentral_redirect -browser;'
                         icon: '$toolbox/matlab/icons/webicon.gif'
    unsupported_DASH_platform: [0x0 double]

                        label: 'Product Page (Web)'
                     callback: 'web http://www.mathworks.com/products/matlab/ -browser;'
                         icon: '$toolbox/matlab/icons/webicon.gif'
    unsupported_DASH_platform: [0x0 double]

Test reading and writing of a large XML file

Read in a large Matlab file, and write it out In order to get "nicest" data structure cell arrays were allowed when reading. Also in order to get output file in similar format as input, preference was set to avoid the 'item' notation.

xmlfile = fullfile(matlabroot, 'help/techdoc/helpindex.xml');
Pref = [];
Pref.Str2Num   = false;   % don't convert strings that look like numbers to numbers
Pref.NoCells   = false;   % allow output to have cell arrays
fprintf('xml_read timing: ');
tic; tree1 = xml_read(xmlfile, Pref); toc;

Pref.StructItem = false;  % do not use item notation with structures
Pref.CellItem   = false;  % do not use item notation with cells
fprintf('xml_write timing: ');
tic; xml_write('test1.xml', tree1, 'index',Pref); toc;
fprintf('tree: \n');
gen_object_display(tree1)
xml_read timing: Elapsed time is 48.346559 seconds.
xml_write timing: Elapsed time is 18.366443 seconds.
tree: 
    indexitem: [1x5249 cell]
    ATTRIBUTE: [1x1 struct]
               version: [1]
tree2 = xml_read ('test1.xml',Pref);
if (length(tree1.indexitem)==length(tree2.indexitem))
  fprintf('tree1.indexitem and tree1.indexitem have the same length\n');
else
  fprintf('tree1.indexitem and tree1.indexitem don''t have the same length\n');
  return
end
tree1.indexitem and tree1.indexitem have the same length
xml_write('test2.xml', tree2, 'index',Pref);
t1=textread('test1.xml','%s');
t2=textread('test2.xml','%s');
if (all(strcmp(t1,t2)))
  fprintf('Files test1.xml and test2.xml are identical\n');
else
  fprintf('Files test1.xml and test2.xml are different\n');
  return
end
Files test1.xml and test2.xml are identical