Changeset 821


Ignore:
Timestamp:
Oct 8, 2014, 9:10:00 PM (9 years ago)
Author:
sommeria
Message:

xmltree_updated

Location:
trunk/src/@xmltree
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/@xmltree/Contents.m

    r723 r821  
    11% XMLTree: XML Toolbox for Matlab.
    2 % Version 1.0  12-Apr-2002
     2% Version 1.2  17-Nov-2004
    33%
    44% XML file I/O.
     
    2727%
    2828% Graphical user interface methods (work in progress).
     29%   editor      - Reimplementation of <view> for Matlab 6+
    2930%   view        - Graphical display of a tree.
    3031%   view_ui     - Useful function for view method.
     
    3839%   xml_findstr - Find one string within another (mexfile)
    3940%
     41% Conversions Matlab <=> XML
     42%   loadxml     -
     43%   savexml     -
     44%   mat2xml     -
     45%   xml2mat     -
     46%   struct2xml  -
     47%
    4048% Demos.
    4149%   xmldemo1    - Create an XML tree from scratch and save it.
     
    4351%   xmldemo3    - Read an XML file, modify some fields and save it.
    4452
    45 % Copyright 2002 Guillaume Flandin - INRIA Sophia Antipolis
    46 % $Revision: 1.0 $
     53% Copyright 2002-2004 Guillaume Flandin <Guillaume@artefact.tk>
     54% $Revision: 1.2 $
  • trunk/src/@xmltree/attributes.m

    r723 r821  
    5454                error(nargchk(3,4,nargin));
    5555                if nargin == 4
    56                         if ~isa(varargin{4},'double') | ...
     56                        if ischar(varargin{4})
     57                                for i=1:length(tree.tree{uid}.attributes)
     58                                        if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key)
     59                                                varargout{1} = tree.tree{uid}.attributes{i}.val;
     60                                                return;
     61                                        end
     62                                end
     63                                varargout{1} = [];
     64                        elseif ~isa(varargin{4},'double') | ...
    5765                           any(varargin{4}>length(tree.tree{uid}.attributes)) | ...
    5866                           any(varargin{4}<1)
    5967                                error('[XMLTree] Invalid attribute indice.');
    60                         end
    61                         if length(varargin{4}) == 1
    62                                 varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};
    6368                        else
    64                                 varargout{1} = {};
    65                                 for i=1:length(varargin{4})
    66                                         varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)};
     69                                if length(varargin{4}) == 1
     70                                        varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};
     71                                else
     72                                        varargout{1} = {};
     73                                        for i=1:length(varargin{4})
     74                                                varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)};
     75                                        end
    6776                                end
    6877                        end
  • trunk/src/@xmltree/branch.m

    r723 r821  
    2222subtree = xmltree;
    2323subtree = set(subtree,root(subtree),'name',tree.tree{uid}.name);
     24%- fix by Piotr Dollar to copy attributes for the root node:
     25subtree = set(subtree,root(subtree),'attributes',tree.tree{uid}.attributes);
    2426
    2527child = children(tree,uid);
  • trunk/src/@xmltree/convert.m

    r805 r821  
    7575                                s = sub_setfield(s,arg{:},'');
    7676                        end
     77            %- saving attributes : does not work with <a t='q'>b</a>
     78            %- but ok with <a t='q'><c>b</c></a>
     79%             attrb = attributes(tree,'get',uid);     %-
     80%             if ~isempty(attrb)                      %-
     81%                 arg2 = {arg{:} 'attributes'};       %-
     82%                 s = sub_setfield(s,arg2{:},attrb);  %-
     83%                         end                                     %-
    7784                case 'chardata'
    7885                        s = sub_setfield(s,arg{:},get(tree,uid,'value'));
     86                        %- convert strings into their Matlab equivalent when possible
     87                        %- e.g. string '3.14159' becomes double scalar 3.14159
     88%             v = get(tree,uid,'value');              %-
     89%                         cv = str2num(v);                        %-
     90%                         if isempty(cv)                          %-
     91%                                 s = sub_setfield(s,arg{:},v);       %-
     92%                         else                                    %-
     93%                                 s = sub_setfield(s,arg{:},cv);      %-
     94%             end                                     %-
    7995                case 'cdata'
    8096                        s = sub_setfield(s,arg{:},get(tree,uid,'value'));
     
    95111                                                s = sub_setfield(s,arg{:},feval(app,get(tree,uid,'value')));
    96112                                        catch,
    97                                                 warning('[Xmltree/convert] Unknown target application');
     113                                                warning('[XMLTREE] Unknown target application');
    98114                                        end
    99115                        end
     
    115131    if (isa(varargin{i}, 'cell'))
    116132        types{i} = '{}';
    117     elseif ischar(varargin{i})
     133    elseif isstr(varargin{i})
    118134        types{i} = '.';
    119135        subs{i} = varargin{i}; %strrep(varargin{i},' ',''); % deblank field name
  • trunk/src/@xmltree/save.m

    r805 r821  
    1 function save(tree, filename)
     1function varargout = save(tree, filename)
    22% XMLTREE/SAVE Save an XML tree in an XML file
    3 % FORMAT save(tree,filename)
     3% FORMAT varargout = save(tree,filename)
    44%
    5 % tree     - XMLTree
    6 % filename - XML output filename
     5% tree      - XMLTree
     6% filename  - XML output filename
     7% varargout - XML string
    78%_______________________________________________________________________
    89%
    910% Convert an XML tree into a well-formed XML string and write it into
    10 % a file or display it (send it to standard output) if no filename is
    11 % given.
    12 %
    13 %-----------------------------------------------------------------------
    14 % SUBFUNCTIONS:
    15 %
    16 % FORMAT print_subtree(tree,fid,order)
    17 % Send tree entity XML formatted string to fid using 'order' blanks
    18 % tree  - XML tree
    19 % fid   - file identifier
    20 % uid   - uid of the current element
    21 % order - order of recursivity
    22 %
    23 % FORMAT str = entity(str)
    24 % Tool to replace input string in internal entities
    25 % str  - string to modify
     11% a file or return it as a string if no filename is provided.
    2612%_______________________________________________________________________
    2713% @(#)save.m                 Guillaume Flandin                 01/07/11
     
    3117prolog = '<?xml version="1.0" ?>\n';
    3218
    33 %- Standard output
    34 if nargin==1
    35         fid = 1;
     19%- Return the XML tree as a string
     20if nargin == 1
     21        varargout{1} = [sprintf(prolog) ...
     22                print_subtree(tree,'',root(tree))];
    3623%- Output specified
    37 elseif nargin==2
     24else
    3825        %- Filename provided
    39         if ischar(filename)
     26        if isstr(filename)
    4027                [fid, msg] = fopen(filename,'w');
    41                 if fid==-1, error(msg);end
     28                if fid==-1, error(msg); end
    4229                if isempty(tree.filename), tree.filename = filename; end
    4330        %- File identifier provided
    4431        elseif isnumeric(filename) & prod(size(filename)) == 1
    4532                fid = filename;
    46                 prolog = ''; %- In this option, do not write any prolog
    47         %- Rubbish provided
     33                prolog = ''; %- With this option, do not write any prolog
    4834        else
    4935                error('[XMLTree] Invalid argument.');
    5036        end
     37        fprintf(fid,prolog);
     38        save_subtree(tree,fid,root(tree));
     39        if isstr(filename), fclose(fid); end
     40        if nargout == 1
     41                varargout{1} = print_subtree(tree,'',root(tree));
     42        end
    5143end
    5244
    53 fprintf(fid,prolog);
    54 print_subtree(tree,fid);
    55 
    56 if nargin==2 && ischar(filename), fclose(fid); end
    57 
    58 if nargout==1, varargout{1} = tree; end
     45%=======================================================================
     46function xmlstr = print_subtree(tree,xmlstr,uid,order)
     47        if nargin < 4, order = 0; end
     48        xmlstr = [xmlstr blanks(3*order)];
     49        switch tree.tree{uid}.type
     50                case 'element'
     51                        xmlstr = sprintf('%s<%s',xmlstr,tree.tree{uid}.name);
     52                        for i=1:length(tree.tree{uid}.attributes)
     53                                xmlstr = sprintf('%s %s="%s"', xmlstr, ...
     54                                        tree.tree{uid}.attributes{i}.key,...
     55                                        tree.tree{uid}.attributes{i}.val);
     56                        end
     57                        if isempty(tree.tree{uid}.contents)
     58                                xmlstr = sprintf('%s/>\n',xmlstr);
     59                        else
     60                                xmlstr = sprintf('%s>\n',xmlstr);
     61                                for i=1:length(tree.tree{uid}.contents)
     62                                        xmlstr = print_subtree(tree,xmlstr, ...
     63                                                tree.tree{uid}.contents(i),order+1);
     64                                end
     65                                xmlstr = [xmlstr blanks(3*order)];
     66                                xmlstr = sprintf('%s</%s>\n',xmlstr,...
     67                                        tree.tree{uid}.name);
     68                        end
     69                case 'chardata'
     70                        xmlstr = sprintf('%s%s\n',xmlstr, ...
     71                                entity(tree.tree{uid}.value));
     72                case 'cdata'
     73                        xmlstr = sprintf('%s<![CDATA[%s]]>\n',xmlstr, ...
     74                                tree.tree{uid}.value);
     75                case 'pi'
     76                        xmlstr = sprintf('%s<?%s %s?>\n',xmlstr, ...
     77                                tree.tree{uid}.target, tree.tree{uid}.value);
     78                case 'comment'
     79                        xmlstr = sprintf('%s<!-- %s -->\n',xmlstr,...
     80                                tree.tree{uid}.value);
     81                otherwise
     82                        warning(sprintf('Type %s unknown: not saved', ...
     83                                tree.tree{uid}.type));
     84        end
    5985
    6086%=======================================================================
    61 function print_subtree(tree,fid,uid,order)
    62         if nargin <3, uid = root(tree); end
     87function save_subtree(tree,fid,uid,order)
    6388        if nargin < 4, order = 0; end
    6489        fprintf(fid,blanks(3*order));
     
    6893                        for i=1:length(tree.tree{uid}.attributes)
    6994                                fprintf(fid,' %s="%s"',...
    70                                 tree.tree{uid}.attributes{i}.key,...
     95                                tree.tree{uid}.attributes{i}.key, ...
    7196                                tree.tree{uid}.attributes{i}.val);
    7297                        end
    73                         fprintf(fid,'>\n');
    74                         for i=1:length(tree.tree{uid}.contents)
    75                                 print_subtree(tree,fid,tree.tree{uid}.contents(i),order+1)
    76                         end
    77                         for i=1:order, fprintf(fid,'   '); end
    78                         fprintf(fid,'</%s>\n',tree.tree{uid}.name);
     98                        if isempty(tree.tree{uid}.contents)
     99                                fprintf(fid,'/>\n');
     100                        else
     101                                fprintf(fid,'>\n');
     102                                for i=1:length(tree.tree{uid}.contents)
     103                                        save_subtree(tree,fid,...
     104                                                tree.tree{uid}.contents(i),order+1)
     105                                end
     106                                fprintf(fid,blanks(3*order));
     107                                fprintf(fid,'</%s>\n',tree.tree{uid}.name);
     108                        end
    79109                case 'chardata'
    80110                        fprintf(fid,'%s\n',entity(tree.tree{uid}.value));
     
    82112                                fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
    83113                case 'pi'
    84                         fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target,tree.tree{uid}.value);
     114                        fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ...
     115                                tree.tree{uid}.value);
    85116                case 'comment'
    86117                        fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value);
    87118                otherwise
    88                         warning(sprintf('Type %s unknown : not saved',tree.tree{uid}.type));
     119                        warning(sprintf('[XMLTree] Type %s unknown: not saved', ...
     120                                tree.tree{uid}.type));
    89121        end
    90    
     122
     123
    91124%=======================================================================
    92125function str = entity(str)
     
    95128        str = strrep(str,'>','&gt;');
    96129        str = strrep(str,'"','&quot;');
    97         str = strrep(str,'\','&apos;');
     130        str = strrep(str,'''','&apos;');
  • trunk/src/@xmltree/xmltree.m

    r723 r821  
    33% FORMAT tree = xmltree(varargin)
    44%
    5 % filename - XML filename
     5% varargin - XML filename or XML string
    66% tree     - XMLTree Object
    77%
    8 %     tree = xmltree;            % creates a minimal XML tree: <tag/>
    9 %     tree = xmltree(filename);  % creates a tree from an XML file
     8%     tree = xmltree;             % creates a minimal XML tree: '<tag/>'
     9%     tree = xmltree('foo.xml');  % creates a tree from XML file 'foo.xml'
     10%     tree = xmltree('<tag>content</tag>') % creates a tree from string
    1011%_______________________________________________________________________
    1112%
     
    3233                        tree = varargin{1};
    3334                elseif ischar(varargin{1})
    34                         tree.tree = xml_parser(varargin{1});
    35                         tree.filename = varargin{1};
     35                        % Input argument is an XML string
     36                        if (exist(varargin{1}) ~= 2 & ...
     37                                ~isempty(xml_findstr(varargin{1},'<',1,1)))
     38                                tree.tree = xml_parser(varargin{1});
     39                                tree.filename = '';
     40                        % Input argument is an XML filename
     41                        else
     42                                fid = fopen(varargin{1},'rt');
     43                                if (fid == -1)
     44                                        error(['[XMLTree] Cannot open ' varargin{1}]);
     45                                end
     46                                xmlstr = fscanf(fid,'%c');
     47                                fclose(fid);
     48                                tree.tree = xml_parser(xmlstr);
     49                                tree.filename = varargin{1};
     50                        end
    3651                        tree = class(tree,'xmltree');
    3752                else
     
    3954                end
    4055        otherwise
    41                 error('[XMLTree] Bad number of arguments');
     56                error('[XMLTree] Too many input arguments');
    4257end
Note: See TracChangeset for help on using the changeset viewer.