Home > . > struct2xml.m

# struct2xml

## PURPOSE

'struct2xml': transform a matlab structure to a xml tree.

## SYNOPSIS

function t=struct2xml(Object,t,root_uid)

## DESCRIPTION

    'struct2xml': transform a matlab structure to a xml tree.
    --------------------------------------------------------------
     each field with char string or num vector is transformed into a corresponding  xml element
     each field with a matrix containing n lines is transformed into a xml element repeated n times
     WARNING: PROBLEM WITH HIERARCHICAL structures
    %%%%%%%%%%%%%%%%%%%%%%
     OUTPUT:
     t: xmltree reproducing the structure of Object
     type 'save(t)' to visualize the xml text and save(filename,t) to save it in a file

     INPUT:
      Object: matlab structure, possibly hierarchical
      t: optional input xml tree in which a new branch needs to be appended
      root_uid: optional uid of the xml element under which the new subtree must be appended

## CROSS-REFERENCE INFORMATION

This function calls:

- struct2xml 'struct2xml': transform a matlab structure to a xml tree.

This function is called by:

- civ_3D 'civ_3D': function associated with the interface 'civ_3D.fig' for PIV in volume
- dataview TEST 'dataview': function for scanning directories in a campaign (TEST)
- geometry_calib 'geometry_calib': performs geometric calibration from a set of reference points
- probe_calib 'probe_calib': performs geometric calibration from a set of reference points
- series 'series': master function associated to the GUI series.m for analysis field series
- set_object 'set_object': GUI to edit a projection object
- struct2xml 'struct2xml': transform a matlab structure to a xml tree.

## SUBFUNCTIONS

- function t=add_element(t,uid,key,val)

## SOURCE CODE

    0001 %'struct2xml': transform a matlab structure to a xml tree.
    0002 %--------------------------------------------------------------
    0003 % each field with char string or num vector is transformed into a corresponding  xml element
    0004 % each field with a matrix containing n lines is transformed into a xml element repeated n times
    0005 % WARNING: PROBLEM WITH HIERARCHICAL structures
    0006 %%%%%%%%%%%%%%%%%%%%%%%
    0007 % OUTPUT:
    0008 % t: xmltree reproducing the structure of Object
    0009 % type 'save(t)' to visualize the xml text and save(filename,t) to save it in a file
    0010 %
    0011 % INPUT:
    0012 %  Object: matlab structure, possibly hierarchical
    0013 %  t: optional input xml tree in which a new branch needs to be appended
    0014 %  root_uid: optional uid of the xml element under which the new subtree must be appended
    0015
    0016 function t=struct2xml(Object,t,root_uid)
    0017 % if ~exist('rootname','var')
    0018 %     rootname='Object';
    0019 % end
    0020
    0021 if ~exist('t','var')
    0022     t=xmltree;
    0023 end
    0024 if ~exist('root_uid','var')
    0025     root_uid=1;
    0026 end
    0027 % if exist('rootname','var')%name the root element with the struct root
    0028 %     if ischar(rootname)
    0029 %         t=set(t,1,'name',rootname);
    0030 %     end
    0031 %     %Faire cas rootname= tree
    0032 %   % eval(['Object=Object.' fieldnames{1}]); %get the substructure
    0033 % end
    0034
    0035     fieldnames=fields(Object);
    0036     for ilist=1:length(fieldnames)
    0037        eval(['val=Object.' fieldnames{ilist} ';'])
    0038        if isstruct(val)
    0039           [t,uid]=add(t,root_uid,'element',fieldnames{ilist});
    0040           fieldnames_sub=fields(val);
    0041           for ilist_sub=1:length(fieldnames_sub)
    0042               if isstruct(fieldnames_sub{ilist_sub})
    0043                     t=struct2xml(fieldnames_sub{ilist_sub},t,uid);
    0044                     save(t)
    0045               else
    0046                   eval(['val_sub=val.' fieldnames_sub{ilist_sub} ';'])
    0047                   t=add_element(t,uid,fieldnames_sub{ilist_sub},val_sub);
    0048               end
    0049           end
    0050        else
    0051            t=add_element(t,root_uid,fieldnames{ilist},val);
    0052        end
    0053     end
    0054
    0055
    0056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0057 function t=add_element(t,uid,key,val)
    0058  if ischar(val)
    0059      [t,new_uid]=add(t,uid,'element',key);
    0060      [t]=add(t,new_uid,'chardata',val);
    0061  elseif isnumeric(val)
    0062        siz=size(val);
    0063        if length(siz)<=2 %do not translate matrices with more than 2 indices
    0064            for iline=1:siz(1)
    0065                 val_str=num2str(val(iline,:));
    0066                 [t,new_uid]=add(t,uid,'element',key);
    0067                 if siz(1)>1
    0068                     t = attributes(t,'add',new_uid,'i',num2str(iline));
    0069                 end
    0070                 [t]=add(t,new_uid,'chardata',val_str);
    0071            end
    0072        end
    0073  end