Changeset 925 for trunk/src


Ignore:
Timestamp:
Feb 17, 2016, 12:52:48 PM (8 years ago)
Author:
sommeria
Message:

xmltree updated

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

Legend:

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

    r821 r925  
    1 % XMLTree: XML Toolbox for Matlab.
    2 % Version 1.2  17-Nov-2004
     1% XMLTree: XML Toolbox for MATLAB and GNU Octave
     2% Version 2.0  14-Aug-2015
    33%
    44% XML file I/O.
     
    1111%   branch      - Extract a subtree from a tree.
    1212%   children    - Return children of a node.
    13 %   convert     - Convert a tree in a Matlab structure.
     13%   convert     - Convert a tree in a MATLAB structure.
    1414%   copy        - Copy nodes within a tree.
    1515%   delete      - Delete a node in a tree.
     
    2424%   root        - Return the root element of a tree.
    2525%   set         - Set node properties.
    26 %   setfilename - Set filename
     26%   setfilename - Set filename.
    2727%
    28 % Graphical user interface methods (work in progress).
    29 %   editor      - Reimplementation of <view> for Matlab 6+
    30 %   view        - Graphical display of a tree.
    31 %   view_ui     - Useful function for view method.
     28% Graphical user interface methods (basic).
     29%   editor      - Graphical display of a tree.
    3230%
    3331% Low level class methods.
    3432%   char        - Convert a tree into a string (for display).
    35 %   display     - Display a tree into MATLAB.
     33%   display     - Display a tree in the workspace.
    3634%
    3735% Private methods.
    3836%   xml_parser  - XML parser.
    39 %   xml_findstr - Find one string within another (mexfile)
     37%   xml_findstr - Find one string within another (C-MEX file).
    4038%
    41 % Conversions Matlab <=> XML
     39% Conversions struct <=> XML.
    4240%   loadxml     -
    4341%   savexml     -
     
    5149%   xmldemo3    - Read an XML file, modify some fields and save it.
    5250
    53 % Copyright 2002-2004 Guillaume Flandin <Guillaume@artefact.tk>
    54 % $Revision: 1.2 $
     51% Copyright 2002-2015  http://www.artefact.tk/
     52
     53% Guillaume Flandin <Guillaume@artefact.tk>
     54% $Id: Contents.m 6480 2015-06-13 01:08:30Z guillaume $
  • trunk/src/@xmltree/add.m

    r723 r925  
    1313%        tree = add(tree,uid,type,parameter);
    1414%        [tree, new_uid] = add(tree,uid,type,parameter);
    15 %_______________________________________________________________________
     15%__________________________________________________________________________
    1616%
    1717% Add a node (element, chardata, cdata, pi or comment) in the XML Tree.
     
    2020% deal with the attributes of an element node (initialized empty).
    2121% The tree parameter must be in input AND in output.
    22 %_______________________________________________________________________
    23 % @(#)add.m                   Guillaume Flandin                02/03/29
     22%__________________________________________________________________________
     23% Copyright (C) 2002-2011  http://www.artefact.tk/
    2424
    25 error(nargchk(4,4,nargin));
     25% Guillaume Flandin
     26% $Id: add.m 4460 2011-09-05 14:52:16Z guillaume $
     27
    2628
    2729if ~isa(uid,'double')
    28         error('[XMLTree] UID must be a double array.');
     30    error('[XMLTree] UID must be a double array.');
    2931end
    3032if ~ischar(type)
    31         error('[XMLTree] TYPE must be a valid item type.');
     33    error('[XMLTree] TYPE must be a valid item type.');
    3234end
    3335if strcmp(type,'pi')
    34         if ~isfield(parameter,'target') | ~isfield(parameter,'value') | ...
    35            ~ischar(parameter.target) | ~ischar(parameter.value)
    36                 error('[XMLTree] For a Processing Instruction, ',...
    37                                                 'PARAMETER must be a struct.');
    38         end
     36    if ~isfield(parameter,'target') || ~isfield(parameter,'value') || ...
     37       ~ischar(parameter.target) || ~ischar(parameter.value)
     38        error(['[XMLTree] For a Processing Instruction, ',...
     39                        'PARAMETER must be a struct.']);
     40    end
    3941elseif ~ischar(parameter)
    40         error('[XMLTree] PARAMETER must be a string.');
     42    error('[XMLTree] PARAMETER must be a string.');
    4143end
    4244
    4345if nargout == 2
    44         l = length(tree.tree);
    45         varargout{2} = (l+1):(l+prod(size(uid)));
     46    l = length(tree.tree);
     47    varargout{2} = (l+1):(l+numel(uid));
    4648end
    4749
    48 for i=1:prod(size(uid))
    49         if uid(i)<1 | uid(i)>length(tree.tree)
    50                 error('[XMLTree] Invalid UID.');
    51         end
    52         if ~strcmp(tree.tree{uid(i)}.type,'element')
    53                 error('[XMLTree] Cannot add a child to a non-element node.');
    54         end
    55         l = length(tree.tree);
    56         switch type
    57                 case 'element'
    58                         tree.tree{l+1} = struct('type','element',...
    59                                         'name',parameter,...
    60                                                         'attributes',[],...
    61                                                         'contents',[],...
    62                                                                         'parent',[],...
    63                                                         'uid',l+1);
    64                 case 'chardata'
    65                         tree.tree{l+1} = struct('type','chardata',...
    66                                                         'value',parameter,...
    67                                                                         'parent',[],...
    68                                                         'uid',l+1);
    69                 case  'cdata'
    70                         tree.tree{l+1} = struct('type','cdata',...
    71                                                         'value',parameter,...
    72                                                                         'parent',[],...
    73                                                         'uid',l+1);
    74                 case 'pi'
    75                         tree.tree{l+1} = struct('type','pi',...
    76                                                                         'target',parameter.target,...
    77                                                         'value',parameter.value,...
    78                                                                         'parent',[],...
    79                                                         'uid',l+1);
    80                 case 'comment'
    81                         tree.tree{l+1} = struct('type','comment',...
    82                                                         'value',parameter,...
    83                                                                         'parent',[],...
    84                                                         'uid',l+1);
    85                 otherwise
    86                         error(sprintf('[XMLTree] %s: unknown item type.',type));
    87         end
    88         tree.tree{uid(i)}.contents = [tree.tree{uid(i)}.contents l+1];
    89         tree.tree{l+1}.parent = uid(i);
     50for i=1:numel(uid)
     51    if uid(i)<1 || uid(i)>length(tree.tree)
     52        error('[XMLTree] Invalid UID.');
     53    end
     54    if ~strcmp(tree.tree{uid(i)}.type,'element')
     55        error('[XMLTree] Cannot add a child to a non-element node.');
     56    end
     57    l = length(tree.tree);
     58    switch type
     59        case 'element'
     60            tree.tree{l+1} = struct('type','element',...
     61                                    'name',parameter,...
     62                                    'attributes',[],...
     63                                    'contents',[],...
     64                                    'parent',[],...
     65                                    'uid',l+1);
     66        case 'chardata'
     67            tree.tree{l+1} = struct('type','chardata',...
     68                                    'value',parameter,...
     69                                    'parent',[],...
     70                                    'uid',l+1);
     71        case  'cdata'
     72            tree.tree{l+1} = struct('type','cdata',...
     73                                    'value',parameter,...
     74                                    'parent',[],...
     75                                    'uid',l+1);
     76        case 'pi'
     77            tree.tree{l+1} = struct('type','pi',...
     78                                    'target',parameter.target,...
     79                                    'value',parameter.value,...
     80                                    'parent',[],...
     81                                    'uid',l+1);
     82        case 'comment'
     83            tree.tree{l+1} = struct('type','comment',...
     84                                    'value',parameter,...
     85                                    'parent',[],...
     86                                    'uid',l+1);
     87        otherwise
     88            error(sprintf('[XMLTree] %s: unknown item type.',type));
     89    end
     90    tree.tree{uid(i)}.contents = [tree.tree{uid(i)}.contents l+1];
     91    tree.tree{l+1}.parent = uid(i);
    9092end
    9193
  • trunk/src/@xmltree/attributes.m

    r821 r925  
    1313%
    1414%     tree = attributes(tree,'set',uid,n,key,val)
    15 %         attr = attributes(tree,'get',uid[,n])
    16 %         tree = attributes(tree,'add',uid,key,val)
    17 %         tree = attributes(tree,'del',uid[,n])
    18 %         l    = attributes(tree,'length',uid)
    19 %_______________________________________________________________________
     15%     attr = attributes(tree,'get',uid[,n])
     16%     tree = attributes(tree,'add',uid,key,val)
     17%     tree = attributes(tree,'del',uid[,n])
     18%     l    = attributes(tree,'length',uid)
     19%__________________________________________________________________________
    2020%
    2121% Handle attributes of an element node.
    2222% The tree parameter must be in input AND in output for 'set', 'add' and
    2323% 'del' methods.
    24 %_______________________________________________________________________
    25 % @(#)attributes.m               Guillaume Flandin             02/04/05
     24%__________________________________________________________________________
     25% Copyright (C) 2002-2011  http://www.artefact.tk/
    2626
    27 error(nargchk(3,6,nargin));
     27% Guillaume Flandin
     28% $Id: attributes.m 4460 2011-09-05 14:52:16Z guillaume $
     29
     30
     31%error(nargchk(3,6,nargin));
     32
    2833tree = varargin{1};
    29 if ~ischar(varargin{2}) | ...
     34if ~ischar(varargin{2}) || ...
    3035   ~any(strcmp(varargin{2},{'set','get','add','del','length'}))
    31         error('[XMLTree] Unknown method.');
     36    error('[XMLTree] Unknown method.');
    3237end
    3338uid = varargin{3};
    34 if ~isa(uid,'double') | any(uid>length(tree)) | any(uid<1)
    35         error('[XMLTree] UID must be a positive integer scalar.');
     39if ~isa(uid,'double') || any(uid>length(tree)) || any(uid<1)
     40    error('[XMLTree] UID must be a positive integer scalar.');
    3641end
    3742
    3843if ~strcmp(tree.tree{uid}.type,'element')
    39         error('[XMLTree] This node has no attributes.');
     44    error('[XMLTree] This node has no attributes.');
    4045end
    4146
    4247switch varargin{2}
    43         case 'set'
    44                 error(nargchk(6,6,nargin));
    45                 if ~isa(varargin{4},'double') | ...
    46                    any(varargin{4}>length(tree.tree{uid}.attributes)) | ...
    47                    any(varargin{4}<1)
    48                         error('[XMLTree] Invalid attribute indice.');
    49                 end
    50                 ind = varargin{4};
    51                 tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6});
    52                 varargout{1} = tree;
    53         case 'get'
    54                 error(nargchk(3,4,nargin));
    55                 if nargin == 4
    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') | ...
    65                            any(varargin{4}>length(tree.tree{uid}.attributes)) | ...
    66                            any(varargin{4}<1)
    67                                 error('[XMLTree] Invalid attribute indice.');
    68                         else
    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
    76                                 end
    77                         end
    78                 else
    79                         if length(tree.tree{uid}.attributes) == 1
    80                                 varargout{1} = tree.tree{uid}.attributes{1};
    81                         else
    82                                 varargout{1} = {};
    83                                 for i=1:length(tree.tree{uid}.attributes)
    84                                         varargout{1}{i} = tree.tree{uid}.attributes{i};
    85                                 end
    86                         end
    87                 end
    88         case 'add'
    89                 error(nargchk(5,5,nargin));
    90                 ind = length(tree.tree{uid}.attributes) + 1;
    91                 tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5});
    92                 varargout{1} = tree;
    93         case 'del'
    94                 error(nargchk(3,4,nargin));
    95                 if nargin == 4
    96                         if ~isa(varargin{4},'double') | ...
    97                       any(varargin{4}>length(tree.tree{uid}.attributes)) | ...
    98                       any(varargin{4}<1)
    99                                 error('[XMLTree] Invalid attribute indice.');
    100                         end
    101                         ind = varargin{4};
    102                         tree.tree{uid}.attributes(ind) = [];
    103                 else
    104                         tree.tree{uid}.attributes = [];
    105                 end
    106                 varargout{1} = tree;
    107         case 'length'
    108                 error(nargchk(3,3,nargin));
    109                 varargout{1} = length(tree.tree{uid}.attributes);
    110         otherwise
    111                 error('[XMLTree] Unknown method.');
     48    case 'set'
     49        %error(nargchk(6,6,nargin));
     50        if ~isa(varargin{4},'double') || ...
     51           any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
     52           any(varargin{4}<1)
     53            error('[XMLTree] Invalid attribute indice.');
     54        end
     55        ind = varargin{4};
     56        tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6});
     57        varargout{1} = tree;
     58    case 'get'
     59        %error(nargchk(3,4,nargin));
     60        if nargin == 4
     61            if ischar(varargin{4})
     62                for i=1:length(tree.tree{uid}.attributes)
     63                    if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key)
     64                        varargout{1} = tree.tree{uid}.attributes{i}.val;
     65                        return;
     66                    end
     67                end
     68                varargout{1} = [];
     69            elseif ~isa(varargin{4},'double') || ...
     70               any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
     71               any(varargin{4}<1)
     72                error('[XMLTree] Invalid attribute indice.');
     73            else
     74                if length(varargin{4}) == 1
     75                    varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};
     76                else
     77                    varargout{1} = {};
     78                    for i=1:length(varargin{4})
     79                        varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)};
     80                    end
     81                end
     82            end
     83        else
     84            if length(tree.tree{uid}.attributes) == 1
     85                varargout{1} = tree.tree{uid}.attributes{1};
     86            else
     87                varargout{1} = {};
     88                for i=1:length(tree.tree{uid}.attributes)
     89                    varargout{1}{i} = tree.tree{uid}.attributes{i};
     90                end
     91            end
     92        end
     93    case 'add'
     94        %error(nargchk(5,5,nargin));
     95        ind = length(tree.tree{uid}.attributes) + 1;
     96        tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5});
     97        varargout{1} = tree;
     98    case 'del'
     99        %error(nargchk(3,4,nargin));
     100        if nargin == 4
     101            if ~isa(varargin{4},'double') || ...
     102              any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
     103              any(varargin{4}<1)
     104                error('[XMLTree] Invalid attribute indice.');
     105            end
     106            ind = varargin{4};
     107            tree.tree{uid}.attributes(ind) = [];
     108        else
     109            tree.tree{uid}.attributes = [];
     110        end
     111        varargout{1} = tree;
     112    case 'length'
     113        %error(nargchk(3,3,nargin));
     114        varargout{1} = length(tree.tree{uid}.attributes);
     115    otherwise
     116        error('[XMLTree] Unknown method.');
    112117end
  • trunk/src/@xmltree/branch.m

    r821 r925  
    66% uid     - UID of the root element of the subtree
    77% subtree - XMLTree object (a subtree from tree)
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    1010% Return a subtree from a tree.
    11 %_______________________________________________________________________
    12 % @(#)branch.m                  Guillaume Flandin              02/04/17
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
    1313
    14 error(nargchk(2,2,nargin));
     14% Guillaume Flandin
     15% $Id: branch.m 4460 2011-09-05 14:52:16Z guillaume $
    1516
    16 if uid > length(tree) | ...
    17    prod(size(uid))~=1 | ...
     17
     18%error(nargchk(2,2,nargin));
     19
     20if uid > length(tree) || ...
     21   numel(uid)~=1 || ...
    1822   ~strcmp(tree.tree{uid}.type,'element')
    19         error('[XMLTree] Invalid UID.');
     23    error('[XMLTree] Invalid UID.');
    2024end
    2125
     
    2832
    2933for i=1:length(child)
    30         l = length(subtree);
    31         subtree = sub_branch(tree,subtree,child(i),root(subtree));
    32         subtree.tree{root(subtree)}.contents = [subtree.tree{root(subtree)}.contents l+1];
     34    l = length(subtree);
     35    subtree = sub_branch(tree,subtree,child(i),root(subtree));
     36    subtree.tree{root(subtree)}.contents = [subtree.tree{root(subtree)}.contents l+1];
    3337end
    3438
    35 %=======================================================================
     39%==========================================================================
    3640function tree = sub_branch(t,tree,uid,p)
    3741
    38         l = length(tree);
    39         tree.tree{l+1} = t.tree{uid};
    40         tree.tree{l+1}.uid = l + 1;
    41         tree.tree{l+1}.parent = p;
    42         tree.tree{l+1}.contents = [];
    43         if isfield(t.tree{uid},'contents')
    44                 contents = get(t,uid,'contents');
    45                 m = length(tree);
    46                 for i=1:length(contents)
    47                         tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
    48                         tree = sub_branch(t,tree,contents(i),l+1);
    49                         m = length(tree);
    50                 end
    51         end
     42    l = length(tree);
     43    tree.tree{l+1} = t.tree{uid};
     44    tree.tree{l+1}.uid = l + 1;
     45    tree.tree{l+1}.parent = p;
     46    tree.tree{l+1}.contents = [];
     47    if isfield(t.tree{uid},'contents')
     48        contents = get(t,uid,'contents');
     49        m = length(tree);
     50        for i=1:length(contents)
     51            tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
     52            tree = sub_branch(t,tree,contents(i),l+1);
     53            m = length(tree);
     54        end
     55    end
  • trunk/src/@xmltree/char.m

    r723 r925  
    55% tree - XMLTree object
    66% s    - a description string of an XMLTree
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Return a string describing the XMLTree:
    1010%               'XMLTree object (x nodes) [filename]'
    11 %_______________________________________________________________________
    12 % @(#)char.m                   Guillaume Flandin               02/04/04
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
     13
     14% Guillaume Flandin
     15% $Id: char.m 4460 2011-09-05 14:52:16Z guillaume $
     16
    1317
    1418s = strcat('XMLTree object (',num2str(length(tree)),' nodes) [',getfilename(tree),']');
  • trunk/src/@xmltree/children.m

    r723 r925  
    66% uid    - uid of the element
    77% child  - array of the UIDs of children of node uid
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    1010% Return UID's of children of node uid
    11 %_______________________________________________________________________
    12 % @(#)children.m              Guillaume Flandin                02/04/09
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
    1313
    14 error(nargchk(2,2,nargin));
     14% Guillaume Flandin
     15% $Id: children.m 4460 2011-09-05 14:52:16Z guillaume $
     16
     17%error(nargchk(2,2,nargin));
    1518
    1619child = [];
     
    1821l = length(tree);
    1922for i=1:length(uid)
    20         if uid(i) > 0 & uid(i) <= l
    21                 if strcmp(tree.tree{uid(i)}.type,'element')
    22                         child = [child tree.tree{uid(i)}.contents];
    23                 end
    24         else
    25                 error('[XMLTree] Invalid UID.');
    26         end
     23    if uid(i) > 0 && uid(i) <= l
     24        if strcmp(tree.tree{uid(i)}.type,'element')
     25            child = [child tree.tree{uid(i)}.contents];
     26        end
     27    else
     28        error('[XMLTree] Invalid UID.');
     29    end
    2730end
    2831if isempty(child), child = []; end
  • trunk/src/@xmltree/convert.m

    r821 r925  
    11function s = convert(tree,uid)
    2 % XMLTREE/CONVERT Converter an XML tree in a Matlab structure
     2% XMLTREE/CONVERT Converter an XML tree in a structure
    33%
    44% tree      - XMLTree object
     
    66%             Default is root
    77% s         - converted structure
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    10 % Convert an xmltree into a Matlab structure, when possible.
     10% Convert an XMLTree into a structure, when possible.
    1111% When several identical tags are present, a cell array is used.
    1212% The root tag is not saved in the structure.
    1313% If provided, only the structure corresponding to the subtree defined
    1414% by the uid UID is returned.
    15 %_______________________________________________________________________
    16 % @(#)convert.m                 Guillaume Flandin              02/04/11
     15%__________________________________________________________________________
     16% Copyright (C) 2002-2015  http://www.artefact.tk/
     17
     18% Guillaume Flandin
     19% $Id: convert.m 6480 2015-06-13 01:08:30Z guillaume $
    1720
    1821% Exemple:
    19 % tree: <toto><titi>field1</titi><tutu>field2</tutu><titi>field3</titi></toto>
    20 % toto = convert(tree);
    21 % <=> toto = struct('titi',{{'field1', 'field3'}},'tutu','field2')
     22% tree = '<a><b>field1</b><c>field2</c><b>field3</b></a>';
     23% toto = convert(xmltree(tree));
     24% <=> toto = struct('b',{{'field1', 'field3'}},'c','field2')
    2225
    23 error(nargchk(1,2,nargin));
     26%error(nargchk(1,2,nargin));
    2427
    2528% Get the root uid of the output structure
    2629if nargin == 1
    27         % Get the root uid of the XML tree
    28         root_uid = root(tree);
     30    % Get the root uid of the XML tree
     31    root_uid = root(tree);
    2932else
    30         % Uid provided by user
    31         root_uid = uid;
     33    % Uid provided by user
     34    root_uid = uid;
    3235end
    3336
     
    4346s = rmfield(s,'deletedummy');
    4447
    45 %=======================================================================
     48%==========================================================================
    4649function s = sub_convert(tree,s,uid,arg)
    47         type = get(tree,uid,'type');
    48         switch type
    49                 case 'element'
    50                         child = children(tree,uid);
    51                         l = {};
    52                         ll = {};
    53                         for i=1:length(child)
    54                                 if isfield(tree,child(i),'name')
    55                                         ll = { ll{:}, get(tree,child(i),'name') };
    56                                 end
    57                         end
    58                         for i=1:length(child)
    59                                 if isfield(tree,child(i),'name')
    60                                         name = get(tree,child(i),'name');
    61                                         nboccur = sum(ismember(l,name));
    62                                         nboccur2 = sum(ismember(ll,name));
    63                                         l = { l{:}, name };
    64                                         if nboccur | (nboccur2>1)
    65                                                 arg2 = { arg{:}, name, {nboccur+1} };
    66                                         else
    67                                                 arg2 = { arg{:}, name};
    68                                         end
    69                                 else
    70                                         arg2 = arg;
    71                                 end
    72                                 s = sub_convert(tree,s,child(i),arg2);
    73                         end
    74                         if isempty(child)
    75                                 s = sub_setfield(s,arg{:},'');
    76                         end
     50    type = get(tree,uid,'type');
     51    switch type
     52        case 'element'
     53            child = children(tree,uid);
     54            l = {};
     55            ll = {};
     56            for i=1:length(child)
     57                if isfield(tree,child(i),'name')
     58                    ll = { ll{:}, get(tree,child(i),'name') };
     59                end
     60            end
     61            for i=1:length(child)
     62                if isfield(tree,child(i),'name')
     63                    name = get(tree,child(i),'name');
     64                    nboccur = sum(ismember(l,name));
     65                    nboccur2 = sum(ismember(ll,name));
     66                    l = { l{:}, name };
     67                    if nboccur || (nboccur2>1)
     68                        arg2 = { arg{:}, name, {nboccur+1} };
     69                    else
     70                        arg2 = { arg{:}, name};
     71                    end
     72                else
     73                    arg2 = arg;
     74                end
     75                s = sub_convert(tree,s,child(i),arg2);
     76            end
     77            if isempty(child)
     78                s = sub_setfield(s,arg{:},'');
     79            end
    7780            %- saving attributes : does not work with <a t='q'>b</a>
    7881            %- but ok with <a t='q'><c>b</c></a>
     
    8184%                 arg2 = {arg{:} 'attributes'};       %-
    8285%                 s = sub_setfield(s,arg2{:},attrb);  %-
    83 %                         end                                     %-
    84                 case 'chardata'
    85                         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
     86%             end                                     %-
     87        case 'chardata'
     88            s = sub_setfield(s,arg{:},get(tree,uid,'value'));
     89            %- convert strings into their numerical equivalent when possible
     90            %- e.g. string '3.14159' becomes double scalar 3.14159
    8891%             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);      %-
     92%             cv = str2num(v);                        %-
     93%             if isempty(cv)                          %-
     94%                 s = sub_setfield(s,arg{:},v);       %-
     95%             else                                    %-
     96%                 s = sub_setfield(s,arg{:},cv);      %-
    9497%             end                                     %-
    95                 case 'cdata'
    96                         s = sub_setfield(s,arg{:},get(tree,uid,'value'));
    97                 case 'pi'
    98                         % Processing instructions are evaluated if possible
    99                         app = get(tree,uid,'target');
    100                         switch app
    101                                 case {'matlab',''}
    102                                         s = sub_setfield(s,arg{:},eval(get(tree,uid,'value')));
    103                                 case 'unix'
    104                                         s = sub_setfield(s,arg{:},unix(get(tree,uid,'value')));
    105                                 case 'dos'
    106                                         s = sub_setfield(s,arg{:},dos(get(tree,uid,'value')));
    107                                 case 'system'
    108                                         s = sub_setfield(s,arg{:},system(get(tree,uid,'value')));
    109                                 otherwise
    110                                         try,
    111                                                 s = sub_setfield(s,arg{:},feval(app,get(tree,uid,'value')));
    112                                         catch,
    113                                                 warning('[XMLTREE] Unknown target application');
    114                                         end
    115                         end
    116                 case 'comment'
    117                         % Comments are forgotten
    118                 otherwise
    119                         warning(sprintf('Type %s unknown : not saved',get(tree,uid,'type')));
    120         end
    121        
    122 %=======================================================================
     98        case 'cdata'
     99            s = sub_setfield(s,arg{:},get(tree,uid,'value'));
     100        case 'pi'
     101            % Processing instructions are evaluated if possible
     102            app = get(tree,uid,'target');
     103            switch app
     104                case {'matlab',''}
     105                    s = sub_setfield(s,arg{:},eval(get(tree,uid,'value')));
     106                case 'unix'
     107                    s = sub_setfield(s,arg{:},unix(get(tree,uid,'value')));
     108                case 'dos'
     109                    s = sub_setfield(s,arg{:},dos(get(tree,uid,'value')));
     110                case 'system'
     111                    s = sub_setfield(s,arg{:},system(get(tree,uid,'value')));
     112                otherwise
     113                    try
     114                        s = sub_setfield(s,arg{:},feval(app,get(tree,uid,'value')));
     115                    catch
     116                        warning('[XMLTree] Unknown target application');
     117                    end
     118            end
     119        case 'comment'
     120            % Comments are forgotten
     121        otherwise
     122            warning(sprintf('Type %s unknown : not saved',get(tree,uid,'type')));
     123    end
     124   
     125%==========================================================================
    123126function s = sub_setfield(s,varargin)
    124127% Same as setfield but using '{}' rather than '()'
     
    131134    if (isa(varargin{i}, 'cell'))
    132135        types{i} = '{}';
    133     elseif isstr(varargin{i})
     136    elseif ischar(varargin{i})
    134137        types{i} = '.';
    135138        subs{i} = varargin{i}; %strrep(varargin{i},' ',''); % deblank field name
  • trunk/src/@xmltree/copy.m

    r723 r925  
    66% subuid    - UID of the subtree to copy
    77% uid       - UID of the element where the subtree must be duplicated
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    10 % Copy a subtree to another branch
    11 % The tree parameter must be in input AND in output
    12 %_______________________________________________________________________
    13 % @(#)copy.m                   Guillaume Flandin               02/04/08
     10% Copy a subtree to another branch.
     11% The tree parameter must be in input AND in output.
     12%__________________________________________________________________________
     13% Copyright (C) 2002-2015  http://www.artefact.tk/
    1414
    15 error(nargchk(2,3,nargin));
     15% Guillaume Flandin
     16% $Id: copy.m 6480 2015-06-13 01:08:30Z guillaume $
     17
     18
     19%error(nargchk(2,3,nargin));
     20
    1621if nargin == 2
    17         uid = parent(tree,subuid);
     22    uid = parent(tree,subuid);
    1823end
    1924
     
    2227tree.tree{uid}.contents = [tree.tree{uid}.contents l+1];
    2328
    24 % pour que la copie soit a cote de l'original et pas a la fin ?
     29% to have the copy next to the original and not at the end?
    2530%  contents = get(tree,parent,'contents');
    2631%  i = find(contents==uid);
    2732%  tree = set(tree,parent,'contents',[contents(1:i) l+1 contents(i+1:end)]);
    2833
    29 %=======================================================================
     34%==========================================================================
    3035function tree = sub_copy(tree,uid,p)
    3136
    32         l = length(tree);
    33         tree.tree{l+1} = tree.tree{uid};
    34         tree.tree{l+1}.uid = l+1;
    35         tree.tree{l+1}.parent = p;
    36         tree.tree{l+1}.contents = [];
    37         if isfield(tree.tree{uid},'contents')
    38                 contents = get(tree,uid,'contents');
    39                 m = length(tree);
    40                 for i=1:length(contents)
    41                         tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
    42                         tree = sub_copy(tree,contents(i),l+1);
    43                         m = length(tree);
    44                 end
    45         end
     37    l = length(tree);
     38    tree.tree{l+1} = tree.tree{uid};
     39    tree.tree{l+1}.uid = l+1;
     40    tree.tree{l+1}.parent = p;
     41    tree.tree{l+1}.contents = [];
     42    if isfield(tree.tree{uid},'contents')
     43        contents = get(tree,uid,'contents');
     44        m = length(tree);
     45        for i=1:length(contents)
     46            tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1];
     47            tree = sub_copy(tree,contents(i),l+1);
     48            m = length(tree);
     49        end
     50    end
  • trunk/src/@xmltree/delete.m

    r723 r925  
    44% tree      - XMLTree object
    55% uid       - array of UID's of subtrees to be deleted
    6 %_______________________________________________________________________
     6%__________________________________________________________________________
    77%
    88% Delete a subtree given its UID
    99% The tree parameter must be in input AND in output
    10 %_______________________________________________________________________
    11 % @(#)delete.m                 Guillaume Flandin               02/04/08
     10%__________________________________________________________________________
     11% Copyright (C) 2002-2011  http://www.artefact.tk/
    1212
    13 error(nargchk(2,2,nargin));
     13% Guillaume Flandin
     14% $Id: delete.m 4460 2011-09-05 14:52:16Z guillaume $
     15
     16%error(nargchk(2,2,nargin));
    1417
    1518uid = uid(:);
    1619for i=1:length(uid)
    17         if uid(i)==1
    18                 warning('[XMLTree] Cannot delete root element.');
    19         else
    20                 p = tree.tree{uid(i)}.parent;
    21                 tree = sub_delete(tree,uid(i));
    22                 tree.tree{p}.contents(find(tree.tree{p}.contents==uid(i))) = [];
    23         end
     20    if uid(i)==1
     21        warning('[XMLTree] Cannot delete root element.');
     22    else
     23        p = tree.tree{uid(i)}.parent;
     24        tree = sub_delete(tree,uid(i));
     25        tree.tree{p}.contents(find(tree.tree{p}.contents==uid(i))) = [];
     26    end
    2427end
    2528
    26 %=======================================================================
     29%==========================================================================
    2730function tree = sub_delete(tree,uid)
    28         if isfield(tree.tree{uid},'contents')
    29                 for i=1:length(tree.tree{uid}.contents)
    30                         tree = sub_delete(tree,tree.tree{uid}.contents(i));
    31                 end
    32         end
    33         tree.tree{uid} = struct('type','deleted');
     31    if isfield(tree.tree{uid},'contents')
     32        for i=1:length(tree.tree{uid}.contents)
     33            tree = sub_delete(tree,tree.tree{uid}.contents(i));
     34        end
     35    end
     36    tree.tree{uid} = struct('type','deleted');
  • trunk/src/@xmltree/display.m

    r723 r925  
    44%
    55% tree - XMLTree object
    6 %_______________________________________________________________________
     6%__________________________________________________________________________
    77%
    88% This method is called when the semicolon is not used to terminate a
    99% statement which returns an XMLTree.
    10 %_______________________________________________________________________
    11 % @(#)display.m                  Guillaume Flandin             02/04/04
     10%__________________________________________________________________________
     11% Copyright (C) 2002-2011  http://www.artefact.tk/
     12
     13% Guillaume Flandin
     14% $Id: display.m 4460 2011-09-05 14:52:16Z guillaume $
    1215
    1316disp(' ');
    1417disp([inputname(1),' = ']);
    1518disp(' ');
    16 for i=1:prod(size(tree))
    17         disp([blanks(length(inputname(1))+3) char(tree(i))]);
     19for i=1:numel(tree)
     20    disp([blanks(length(inputname(1))+3) char(tree(i))]);
    1821end
    1922disp(' ');
  • trunk/src/@xmltree/find.m

    r723 r925  
    1616%        XML Path Language XPath (http://www.w3.org/TR/xpath)
    1717% Example: /element1//element2[1]/element3[5]/element4
    18 %_______________________________________________________________________
     18%__________________________________________________________________________
    1919%
    2020% Find elements in an XML tree with specified characteristics or given
    2121% a path (using a subset of XPath language).
    22 %_______________________________________________________________________
    23 % @(#)find.m                 Guillaume Flandin                 01/10/29
     22%__________________________________________________________________________
     23% Copyright (C) 2002-2011  http://www.artefact.tk/
     24
     25% Guillaume Flandin
     26% $Id: find.m 4460 2011-09-05 14:52:16Z guillaume $
    2427
    2528% TODO:
     
    3033
    3134if nargin==0
    32         error('[XMLTree] A tree must be provided');
     35    error('[XMLTree] A tree must be provided');
    3336elseif nargin==1
    34         list = 1:length(tree.tree);
    35         return
     37    list = 1:length(tree.tree);
     38    return
    3639elseif mod(nargin,2)
    37         list = sub_find_subtree1(varargin{1}.tree,root(tree),varargin{2:end});
    38 elseif isa(varargin{2},'double') & ...
    39            ndims(varargin{2}) == 2 & ...
    40            min(size(varargin{2})) == 1
    41         list = unique(sub_find_subtree1(varargin{1}.tree,varargin{2:end}));
    42 elseif nargin==2 & ischar(varargin{2})
    43         list = sub_pathfinder(varargin{:});
     40    list = sub_find_subtree1(varargin{1}.tree,root(varargin{1}),varargin{2:end});
     41elseif isa(varargin{2},'double') && ...
     42       ndims(varargin{2}) == 2 && ...
     43       min(size(varargin{2})) == 1
     44    list = unique(sub_find_subtree1(varargin{1}.tree,varargin{2:end}));
     45elseif nargin==2 && ischar(varargin{2})
     46    list = sub_pathfinder(varargin{:});
    4447else
    4548   error('[XMLTree] Arguments must be parameter/value pairs.');
    4649end
    4750
    48 %=======================================================================
     51%==========================================================================
    4952function list = sub_find_subtree1(varargin)
    50         list = [];
    51         for i=1:length(varargin{2})
    52                 res = sub_find_subtree2(varargin{1},...
    53                                 varargin{2}(i),varargin{3:end});
    54                 list = [list res];
    55         end
     53    list = [];
     54    for i=1:length(varargin{2})
     55        res = sub_find_subtree2(varargin{1},...
     56                varargin{2}(i),varargin{3:end});
     57        list = [list res];
     58    end
    5659
    57 %=======================================================================
     60%==========================================================================
    5861function list = sub_find_subtree2(varargin)
    59         uid = varargin{2};
    60         list = [];
    61         if sub_comp_element(varargin{1}{uid},varargin{3:end})
    62                 list = [list varargin{1}{uid}.uid];
    63         end
    64         if isfield(varargin{1}{uid},'contents')
    65                 list = [list sub_find_subtree1(varargin{1},...
    66                         varargin{1}{uid}.contents,varargin{3:end})];
    67         end
     62    uid = varargin{2};
     63    list = [];
     64    if sub_comp_element(varargin{1}{uid},varargin{3:end})
     65        list = [list varargin{1}{uid}.uid];
     66    end
     67    if isfield(varargin{1}{uid},'contents')
     68        list = [list sub_find_subtree1(varargin{1},...
     69                varargin{1}{uid}.contents,varargin{3:end})];
     70    end
    6871
    69 %=======================================================================
     72%==========================================================================
    7073function match = sub_comp_element(varargin)
    7174match = 0;
    72 try,
    73         % v = getfield(varargin{1}, varargin{2}); % slow...
    74         for i=1:floor(nargin/2)
    75                 v = subsref(varargin{1}, struct('type','.','subs',varargin{i+1}));     
    76                 if strcmp(v,varargin{i+2})
    77                         match = 1;
    78                 end
    79         end
    80 catch,
     75try
     76    % v = getfield(varargin{1}, varargin{2}); % slow...
     77    for i=1:floor(nargin/2)
     78        v = subsref(varargin{1}, struct('type','.','subs',varargin{i+1}));     
     79        if strcmp(v,varargin{i+2})
     80            match = 1;
     81        end
     82    end
     83catch
    8184end
    8285
     
    8588%for i=1:length(floor(nargin/2)) % bug: remove length !!!
    8689%   if isfield(varargin{1},varargin{i+1})
    87 %         if ischar(getfield(varargin{1},varargin{i+1})) & ischar(varargin{i+2})
    88 %          if strcmp(getfield(varargin{1},varargin{i+1}),char(varargin{i+2}))
    89 %                       match = 1;
    90 %                end
    91 %         elseif isa(getfield(varargin{1},varargin{i+1}),'double') & ...
    92 %                       isa(varargin{i+2},'double')
    93 %                if getfield(varargin{1},varargin{i+1}) == varargin{i+2}
    94 %                       match = 1;
    95 %               end
    96 %        else
    97 %               warning('Cannot compare different objects');
    98 %         end
     90%     if ischar(getfield(varargin{1},varargin{i+1})) & ischar(varargin{i+2})
     91%      if strcmp(getfield(varargin{1},varargin{i+1}),char(varargin{i+2}))
     92%           match = 1;
     93%        end
     94%     elseif isa(getfield(varargin{1},varargin{i+1}),'double') & ...
     95%           isa(varargin{i+2},'double')
     96%        if getfield(varargin{1},varargin{i+1}) == varargin{i+2}
     97%           match = 1;
     98%       end
     99%    else
     100%       warning('Cannot compare different objects');
     101%     end
    99102%   end
    100103%end
    101104
    102 %=======================================================================
     105%==========================================================================
    103106function list = sub_pathfinder(tree,pth)
    104         %- Search for the delimiter '/' in the path
    105         i = findstr(pth,'/');
    106         %- Begin search by root
    107         list = root(tree);
    108         %- Walk through the tree
    109         j = 1;
    110         while j <= length(i)
    111                 %- Look for recursion '//'
    112                 if j<length(i) & i(j+1)==i(j)+1
    113                         recursive = 1;
    114                         j = j + 1;
    115                 else
    116                         recursive = 0;
    117                 end
    118                 %- Catch the current tag 'element[x]'
    119                 if j ~= length(i)
    120                         element = pth(i(j)+1:i(j+1)-1);
    121                 else
    122                         element = pth(i(j)+1:end);
    123                 end
    124                 %- Search for [] brackets
    125                 k = xml_findstr(element,'[',1,1);
    126                 %- If brackets are present in current element
    127                 if ~isempty(k)
    128                         l   = xml_findstr(element,']',1,1);
    129                         val = str2num(element(k+1:l-1));
    130                         element = element(1:k-1);
    131                 end
    132                 %- Use recursivity
    133                 if recursive
    134                         list = find(tree,list,'name',element);
    135                 %- Just look at children
    136                 else
    137                         if i(j)==1 % if '/root/...' (list = root(tree) in that case)
    138                                 if sub_comp_element(tree.tree{list},'name',element)
    139                                         % list = 1; % list still contains root(tree)
    140                                 else
    141                                         list = [];
    142                                         return;
    143                                 end
    144                         else
    145                                 list = sub_findchild(tree,list,element);
    146                         end
    147                 end
    148                 % If an element is specified using a key
    149                 if ~isempty(k)
    150                         if val < 1 | val > length(list)+1
    151                                 error('[XMLTree] Bad key in the path.');
    152                         elseif val == length(list)+1
    153                                 list = [];
    154                                 return;
    155                         end
    156                         list = list(val);
    157                 end
    158                 if isempty(list), return; end
    159                 j = j + 1;
    160         end
    161        
    162 %=======================================================================
     107    %- Search for the delimiter '/' in the path
     108    i = strfind(pth,'/');
     109    %- Begin search by root
     110    list = root(tree);
     111    %- Walk through the tree
     112    j = 1;
     113    while j <= length(i)
     114        %- Look for recursion '//'
     115        if j<length(i) && i(j+1)==i(j)+1
     116            recursive = 1;
     117            j = j + 1;
     118        else
     119            recursive = 0;
     120        end
     121        %- Catch the current tag 'element[x]'
     122        if j ~= length(i)
     123            element = pth(i(j)+1:i(j+1)-1);
     124        else
     125            element = pth(i(j)+1:end);
     126        end
     127        %- Search for [] brackets
     128        k = xml_findstr(element,'[',1,1);
     129        %- If brackets are present in current element
     130        if ~isempty(k)
     131            l   = xml_findstr(element,']',1,1);
     132            val = str2num(element(k+1:l-1));
     133            element = element(1:k-1);
     134        end
     135        %- Use recursivity
     136        if recursive
     137            list = find(tree,list,'name',element);
     138        %- Just look at children
     139        else
     140            if i(j)==1 % if '/root/...' (list = root(tree) in that case)
     141                if sub_comp_element(tree.tree{list},'name',element)
     142                    % list = 1; % list still contains root(tree)
     143                else
     144                    list = [];
     145                    return;
     146                end
     147            else
     148                list = sub_findchild(tree,list,element);
     149            end
     150        end
     151        % If an element is specified using a key
     152        if ~isempty(k)
     153            if val < 1 || val > length(list)+1
     154                error('[XMLTree] Bad key in the path.');
     155            elseif val == length(list)+1
     156                list = [];
     157                return;
     158            end
     159            list = list(val);
     160        end
     161        if isempty(list), return; end
     162        j = j + 1;
     163    end
     164   
     165%==========================================================================
    163166function list = sub_findchild(tree,listt,elmt)
    164         list = [];
    165         for a=1:length(listt)
    166                 for b=1:length(tree.tree{listt(a)}.contents)
    167                         if sub_comp_element(tree.tree{tree.tree{listt(a)}.contents(b)},'name',elmt)
    168                                 list = [list tree.tree{tree.tree{listt(a)}.contents(b)}.uid];
    169                         end
    170                 end
    171         end
     167    list = [];
     168    for a=1:length(listt)
     169        for b=1:length(tree.tree{listt(a)}.contents)
     170            if sub_comp_element(tree.tree{tree.tree{listt(a)}.contents(b)},'name',elmt)
     171                list = [list tree.tree{tree.tree{listt(a)}.contents(b)}.uid];
     172            end
     173        end
     174    end
  • trunk/src/@xmltree/flush.m

    r723 r925  
    55% uid       - array of UID's of subtrees to be cleared
    66%             Default is root
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Clear a subtree given its UID (remove all the leaves of the tree)
    1010% The tree parameter must be in input AND in output
    11 %_______________________________________________________________________
    12 % @(#)flush.m                  Guillaume Flandin               02/04/10
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
    1313
    14 error(nargchk(1,2,nargin));
     14% Guillaume Flandin
     15% $Id: flush.m 4460 2011-09-05 14:52:16Z guillaume $
     16
     17
     18%error(nargchk(1,2,nargin));
    1519
    1620if nargin == 1,
    17         uid = root(tree);
     21    uid = root(tree);
    1822end
    1923
    2024uid = uid(:);
    2125for i=1:length(uid)
    22         tree = sub_flush(tree,uid(i));
     26    tree = sub_flush(tree,uid(i));
    2327end
    2428
    25 %=======================================================================
     29%==========================================================================
    2630function tree = sub_flush(tree,uid)
    27         if isfield(tree.tree{uid},'contents')
    28                 % contents is parsed in reverse order because each child is
    29                 % deleted and the contents vector is then eventually reduced
    30                 for i=length(tree.tree{uid}.contents):-1:1
    31                         tree = sub_flush(tree,tree.tree{uid}.contents(i));
    32                 end
    33         end
    34         if strcmp(tree.tree{uid}.type,'chardata') |...
    35                 strcmp(tree.tree{uid}.type,'pi') |...
    36                 strcmp(tree.tree{uid}.type,'cdata') |...
    37                 strcmp(tree.tree{uid}.type,'comment')
    38                 tree = delete(tree,uid);
    39         end
     31    if isfield(tree.tree{uid},'contents')
     32        % contents is parsed in reverse order because each child is
     33        % deleted and the contents vector is then eventually reduced
     34        for i=length(tree.tree{uid}.contents):-1:1
     35            tree = sub_flush(tree,tree.tree{uid}.contents(i));
     36        end
     37    end
     38    if strcmp(tree.tree{uid}.type,'chardata') ||...
     39        strcmp(tree.tree{uid}.type,'pi') ||...
     40        strcmp(tree.tree{uid}.type,'cdata') ||...
     41        strcmp(tree.tree{uid}.type,'comment')
     42        tree = delete(tree,uid);
     43    end
  • trunk/src/@xmltree/get.m

    r723 r925  
    77% parameter - property name
    88% value     - property value
    9 %_______________________________________________________________________
     9%__________________________________________________________________________
    1010%
    1111% Get object properties of a tree given their UIDs.
    12 %_______________________________________________________________________
    13 % @(#)get.m                   Guillaume Flandin                02/03/27
     12%__________________________________________________________________________
     13% Copyright (C) 2002-2011  http://www.artefact.tk/
    1414
    15 error(nargchk(2,3,nargin));
     15% Guillaume Flandin
     16% $Id: get.m 4460 2011-09-05 14:52:16Z guillaume $
     17
     18
     19%error(nargchk(2,3,nargin));
    1620
    1721value = cell(size(uid));
    1822uid = uid(:);
    1923if nargin==2
    20         for i=1:length(uid)
    21                 if uid(i)<1 | uid(i)>length(tree.tree)
    22                         error('[XMLTree] Invalid UID.');
    23                 end
    24                 % According to the type of the node, return only some parameters
    25                 % Need changes...
    26                 value{i} = tree.tree{uid(i)};
    27         end
     24    for i=1:length(uid)
     25        if uid(i)<1 || uid(i)>length(tree.tree)
     26            error('[XMLTree] Invalid UID.');
     27        end
     28        % According to the type of the node, return only some parameters
     29        % Need changes...
     30        value{i} = tree.tree{uid(i)};
     31    end
    2832else
    29         for i=1:length(uid)
    30                 try,
    31                         value{i} = subsref(tree.tree{uid(i)}, struct('type','.','subs',parameter));
    32                 catch,
    33                         error(sprintf('[XMLTree] Parameter %s not found.',parameter));
    34                 end
    35         end
     33    for i=1:length(uid)
     34        try
     35            value{i} = subsref(tree.tree{uid(i)}, struct('type','.','subs',parameter));
     36        catch
     37            error(sprintf('[XMLTree] Parameter %s not found.',parameter));
     38        end
     39    end
    3640end
    3741if length(value)==1
    38         value = value{1};
     42    value = value{1};
    3943end 
  • trunk/src/@xmltree/getfilename.m

    r723 r925  
    55% tree     - XMLTree object
    66% filename - XML filename
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Return the filename of the XML tree if loaded from disk and an empty
    1010% string otherwise.
    11 %_______________________________________________________________________
    12 % @(#)getfilename.m               Guillaume Flandin            02/03/27
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
     13
     14% Guillaume Flandin
     15% $Id: getfilename.m 4460 2011-09-05 14:52:16Z guillaume $
    1316
    1417filename = tree.filename;
  • trunk/src/@xmltree/isfield.m

    r723 r925  
    77% parameter - a field of the root tree
    88% F         - 1 if present, 0 otherwise
    9 %_______________________________________________________________________
     9%__________________________________________________________________________
    1010%
    1111% Is parameter a field of tree{uid} ?
    12 %_______________________________________________________________________
    13 % @(#)isfield.m               Guillaume Flandin                01/10/31
     12%__________________________________________________________________________
     13% Copyright (C) 2002-2011  http://www.artefact.tk/
    1414
    15 error(nargchk(3,3,nargin));
     15% Guillaume Flandin
     16% $Id: isfield.m 4460 2011-09-05 14:52:16Z guillaume $
     17
     18
     19%error(nargchk(3,3,nargin));
    1620
    1721F = zeros(1,length(uid));
    1822for i=1:length(uid)
    19         if isfield(tree.tree{uid(i)},parameter)
    20                 F(i) = 1;
    21         end
     23    if isfield(tree.tree{uid(i)},parameter)
     24        F(i) = 1;
     25    end
    2226end
  • trunk/src/@xmltree/length.m

    r723 r925  
    77%         tree (deleted nodes aren't populated)
    88% l    - length of the XML tree (number of nodes)
    9 %_______________________________________________________________________
     9%__________________________________________________________________________
    1010%
    1111% Return the number of nodes of an XMLTree object.
    12 %_______________________________________________________________________
    13 % @(#)length.m                 Guillaume Flandin               02/03/27
     12%__________________________________________________________________________
     13% Copyright (C) 2002-2011  http://www.artefact.tk/
    1414
    15 error(nargchk(1,2,nargin));
     15% Guillaume Flandin
     16% $Id: length.m 4460 2011-09-05 14:52:16Z guillaume $
     17
     18
     19%error(nargchk(1,2,nargin));
    1620
    1721% Return the full number of nodes once allocated
     
    2024% Substract the number of deleted nodes to the previous length
    2125if nargin == 2
    22         if strcmp(r,'real')
    23                 ll = 0;
    24                 for i=1:l
    25                         if ~strcmp(tree.tree{i}.type,'deleted')
    26                                 ll = ll + 1;
    27                         end
    28                 end
    29                 l = ll;
    30         else
    31                 error('[XMLTree] Bad input argument.');
    32         end
     26    if strcmp(r,'real')
     27        ll = 0;
     28        for i=1:l
     29            if ~strcmp(tree.tree{i}.type,'deleted')
     30                ll = ll + 1;
     31            end
     32        end
     33        l = ll;
     34    else
     35        error('[XMLTree] Bad input argument.');
     36    end
    3337end
  • trunk/src/@xmltree/move.m

    r723 r925  
    55% uida   - initial position of the subtree
    66% uidb   - parent of the final position of the subtree
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Move a subtree inside a tree from A to B.
    10 % The tree parameter must be in input AND in output
    11 %_______________________________________________________________________
    12 % @(#)move.m                  Guillaume Flandin                02/04/08
     10% The tree parameter must be in input AND in output.
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2015  http://www.artefact.tk/
    1313
    14 error(nargchk(3,3,nargin));
     14% Guillaume Flandin
     15% $Id: move.m 6480 2015-06-13 01:08:30Z guillaume $
     16
     17
     18%error(nargchk(3,3,nargin));
    1519
    1620p = tree.tree{uida}.parent;
  • trunk/src/@xmltree/parent.m

    r723 r925  
    66% uid    - UID of the lonely child
    77% p      - UID of the parent ([] if root is the child)
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    1010% Return the uid of the parent of a node.
    11 %_______________________________________________________________________
    12 % @(#)parent.m                  Guillaume Flandin              02/04/08
     11%__________________________________________________________________________
     12% Copyright (C) 2002-2011  http://www.artefact.tk/
     13
     14% Guillaume Flandin
     15% $Id: parent.m 4460 2011-09-05 14:52:16Z guillaume $
    1316
    1417p = tree.tree{uid}.parent;
  • trunk/src/@xmltree/private/xml_findstr.c

    r820 r925  
    22
    33/*
    4     Differences with matlab built-in findstr:
     4 * $Id: xml_findstr.c 6480 2015-06-13 01:08:30Z guillaume $
     5 * Guillaume Flandin <guillaume@artefact.tk>
     6 */
     7
     8/*
     9    Differences with built-in findstr:
    510        - allows to search only the n first occurences of a pattern
    611        - allows to search only in a substring (given an index of the beginning)
    712   
    8     Matlab hack:
     13    MATLAB hack:
    914        - doesn't use mxGetString to prevent a copy of the string.
    10         - assumes Matlab stores strings as unsigned short (Unicode 16 bits)
     15        - assumes MATLAB stores strings as unsigned short (Unicode 16 bits)
    1116          matrix.h: typedef uint16_T mxChar;
    12           (that's the case for Matlab 5.* and 6.* but Matlab 4.* stores strings
    13            as double)
     17          (that's the case for MATLAB 5.*, 6.* and 7.* but MATLAB 4.* stores
     18           strings as double and GNU Octave as char, see src/mxarray.h)
    1419*/
    1520
    1621/* Comment the following line to use standard mxGetString (slower) */
     22#if !defined (HAVE_OCTAVE)
    1723#define __HACK_MXCHAR__
     24#endif
    1825
    1926void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
     
    2835    mxArray *out = NULL;
    2936   
    30         /* Check for proper number of arguments. */
     37    /* Check for proper number of arguments. */
    3138    if ((nrhs == 0) || (nrhs == 1))
    32             mexErrMsgTxt("Not enough input arguments.");
     39        mexErrMsgTxt("Not enough input arguments.");
    3340    else if (nrhs > 4)
    34             mexErrMsgTxt("Too many input arguments.");
     41        mexErrMsgTxt("Too many input arguments.");
    3542    else if (nlhs > 1)
    3643        mexErrMsgTxt("Too many output arguments.");
    3744   
    3845    /* The input TEXT must be a string */
    39         if (!mxIsChar(prhs[0]))
    40             mexErrMsgTxt("Inputs must be character arrays.");
    41         stext = mxGetM(prhs[0]) * mxGetN(prhs[0]);
     46    if (!mxIsChar(prhs[0]))
     47        mexErrMsgTxt("Inputs must be character arrays.");
     48    stext = mxGetM(prhs[0]) * mxGetN(prhs[0]);
    4249#ifdef __HACK_MXCHAR__
    43         text = mxGetData(prhs[0]);
     50    text = mxGetData(prhs[0]);
    4451#else
    4552    text = mxCalloc(stext+1, sizeof(char));
     
    4855       
    4956    /* The input PATTERN must be a string */
    50         if (!mxIsChar(prhs[1]))
    51                 mexErrMsgTxt("Inputs must be character arrays.");
     57    if (!mxIsChar(prhs[1]))
     58        mexErrMsgTxt("Inputs must be character arrays.");
    5259    spattern = mxGetM(prhs[1]) * mxGetN(prhs[1]);
    5360#ifdef __HACK_MXCHAR__
    54         pattern = mxGetData(prhs[1]);
     61    pattern = mxGetData(prhs[1]);
    5562#else
    5663    pattern = mxCalloc(spattern+1, sizeof(char));
    57         mxGetString(prhs[1], pattern, spattern+1);
     64    mxGetString(prhs[1], pattern, spattern+1);
    5865#endif
    5966
    60         /* The input INDEX must be an integer */
    61         if (nrhs > 2) {
    62             if ((!mxIsNumeric(prhs[2]) || (mxGetM(prhs[2]) * mxGetN(prhs[2]) !=  1)))
    63                 mexErrMsgTxt("Index input must be an integer.");
    64             ind = (unsigned int)mxGetScalar(prhs[2]);
    65             if (ind < 1)
    66                 mexErrMsgTxt("Index must be greater than 1.");
    67         }
    68        
    69         /* The input OCCUR must be an integer */
    70         if (nrhs == 4) {
    71             if ((!mxIsNumeric(prhs[3]) || (mxGetM(prhs[3]) * mxGetN(prhs[3]) !=  1)))
    72                 mexErrMsgTxt("Index input must be an integer.");
    73             nboccur = (unsigned int)mxGetScalar(prhs[3]);
    74         }
    75        
    76         /* Find pattern in text */
     67    /* The input INDEX must be an integer */
     68    if (nrhs > 2) {
     69        if ((!mxIsNumeric(prhs[2]) || (mxGetM(prhs[2]) * mxGetN(prhs[2]) !=  1)))
     70            mexErrMsgTxt("Index input must be an integer.");
     71        ind = (unsigned int)mxGetScalar(prhs[2]);
     72        if (ind < 1)
     73            mexErrMsgTxt("Index must be greater than 1.");
     74    }
     75   
     76    /* The input OCCUR must be an integer */
     77    if (nrhs == 4) {
     78        if ((!mxIsNumeric(prhs[3]) || (mxGetM(prhs[3]) * mxGetN(prhs[3]) !=  1)))
     79            mexErrMsgTxt("Index input must be an integer.");
     80        nboccur = (unsigned int)mxGetScalar(prhs[3]);
     81    }
     82   
     83    /* Find pattern in text */
    7784    for (i=ind-1;i<stext;i++) {
    7885        for (j=0;j<spattern && i+j<stext;j++) {
  • trunk/src/@xmltree/private/xml_findstr.m

    r820 r925  
    2020%
    2121%   See also STRFIND, FINDSTR
    22 %_______________________________________________________________________
    23 % Copyright (C) 2002-2008  http://www.artefact.tk/
     22%__________________________________________________________________________
     23% Copyright (C) 2002-2011  http://www.artefact.tk/
    2424
    25 % Guillaume Flandin <guillaume@artefact.tk>
    26 % $Id: xml_findstr.m 2271 2008-09-30 21:19:47Z guillaume $
     25% Guillaume Flandin
     26% $Id: xml_findstr.m 4460 2011-09-05 14:52:16Z guillaume $
    2727
    2828%error(sprintf('Missing MEX-file: %s', mfilename));
  • trunk/src/@xmltree/private/xml_parser.m

    r820 r925  
    55% xmlstr  - XML string to parse
    66% tree    - tree structure corresponding to the XML file
    7 %_______________________________________________________________________
    8 %
    9 % xml_parser.m is an XML 1.0 (http://www.w3.org/TR/REC-xml) parser
    10 % written in Matlab. It aims to be fully conforming. It is currently not
    11 % a validating XML processor.
     7%__________________________________________________________________________
     8%
     9% xml_parser.m is an XML 1.0 (http://www.w3.org/TR/REC-xml) parser.
     10% It aims to be fully conforming. It is currently not a validating
     11% XML processor.
    1212%
    1313% A description of the tree structure provided in output is detailed in
    1414% the header of this m-file.
    15 %_______________________________________________________________________
    16 % @(#)xml_parser.m              Guillaume Flandin            2002/04/04
    17 
    18 % XML Processor for MATLAB (The Mathworks, Inc.).
    19 % Copyright (C) 2002-2003 Guillaume Flandin <Guillaume@artefact.tk>
     15%__________________________________________________________________________
     16% Copyright (C) 2002-2015  http://www.artefact.tk/
     17
     18% Guillaume Flandin
     19% $Id: xml_parser.m 6480 2015-06-13 01:08:30Z guillaume $
     20
     21% XML Processor for GNU Octave and MATLAB (The Mathworks, Inc.)
     22% Copyright (C) 2002-2015 Guillaume Flandin <Guillaume@artefact.tk>
    2023%
    2124% This program is free software; you can redistribute it and/or
     
    3235% along with this program; if not, write to the Free Software
    3336% Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA.
    34 %-----------------------------------------------------------------------
     37%--------------------------------------------------------------------------
    3538
    3639% Suggestions for improvement and fixes are always welcome, although no
     
    3942% Check also the latest developments on the following webpage:
    4043%           <http://www.artefact.tk/software/matlab/xml/>
    41 %-----------------------------------------------------------------------
     44%--------------------------------------------------------------------------
    4245
    4346% The implementation of this XML parser is much inspired from a
    44 % Javascript parser available at <http://www.jeremie.com/>
    45 
    46 % A mex-file xml_findstr.c is also required, to encompass some
    47 % limitations of the built-in findstr Matlab function.
     47% Javascript parser that used to be available at <http://www.jeremie.com/>
     48
     49% A C-MEX file xml_findstr.c is also required, to encompass some
     50% limitations of the built-in FINDSTR function.
    4851% Compile it on your architecture using 'mex -O xml_findstr.c' command
    4952% if the compiled version for your system is not provided.
    50 % If this function behaves badly (crash or wrong results), comment the
    51 % line '#define __HACK_MXCHAR__' in xml_findstr.c and compile it again.
    52 %-----------------------------------------------------------------------
     53% If this function does not behave as expected, comment the line
     54% '#define __HACK_MXCHAR__' in xml_findstr.c and compile it again.
     55%--------------------------------------------------------------------------
    5356
    5457% Structure of the output tree:
     
    9194%       |_ uid:    double
    9295%
    93 %-----------------------------------------------------------------------
     96%--------------------------------------------------------------------------
    9497
    9598% TODO/BUG/FEATURES:
     
    103106%  - xml_findstr is indeed xml_strfind according to Mathworks vocabulary
    104107%  - problem with entities: do we need to convert them here? (&eacute;)
    105 %-----------------------------------------------------------------------
     108%--------------------------------------------------------------------------
    106109
    107110%- XML string to parse and number of tags read
     
    109112
    110113%- Check input arguments
    111 error(nargchk(1,1,nargin));
     114%error(nargchk(1,1,nargin));
    112115if isempty(xmlstr)
    113         error('[XML] Not enough parameters.')
    114 elseif ~isstr(xmlstr) | sum(size(xmlstr)>1)>1
    115         error('[XML] Input must be a string.')
     116    error('[XML] Not enough parameters.')
     117elseif ~ischar(xmlstr) || sum(size(xmlstr)>1)>1
     118    error('[XML] Input must be a string.')
    116119end
    117120
     
    137140clear global xmlstring Xparse_count xtree;
    138141
    139 %=======================================================================
     142%==========================================================================
    140143% SUBFUNCTIONS
    141144
    142 %-----------------------------------------------------------------------
     145%--------------------------------------------------------------------------
    143146function frag = compile(frag)
    144         global xmlstring xtree Xparse_count;
    145        
    146         while 1,
    147                 if length(xmlstring)<=frag.str | ...
    148                    (frag.str == length(xmlstring)-1 & strcmp(xmlstring(frag.str:end),' '))
    149                         return
    150                 end
    151                 TagStart = xml_findstr(xmlstring,'<',frag.str,1);
    152                 if isempty(TagStart)
    153                         %- Character data
    154                         error(sprintf(['[XML] Unknown data at the end of the XML file.\n' ...
    155                         '      Please send me your XML file at Guillaume@artefact.tk']));
    156                         xtree{Xparse_count} = chardata;
    157                         xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:end)));
    158                         xtree{Xparse_count}.parent = frag.parent;
    159                         xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    160                         frag.str = '';
    161                 elseif TagStart > frag.str
    162                         if strcmp(xmlstring(frag.str:TagStart-1),' ')
    163                                 %- A single white space before a tag (ignore)
    164                                 frag.str = TagStart;
    165                         else
    166                                 %- Character data
    167                                 xtree{Xparse_count} = chardata;
    168                                 xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:TagStart-1)));
    169                                 xtree{Xparse_count}.parent = frag.parent;
    170                                 xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    171                                 frag.str = TagStart;
    172                         end
    173                 else
    174                         if strcmp(xmlstring(frag.str+1),'?')
    175                                 %- Processing instruction
    176                                 frag = tag_pi(frag);
    177                         else
    178                                 if length(xmlstring)-frag.str>4 & strcmp(xmlstring(frag.str+1:frag.str+3),'!--')
    179                                         %- Comment
    180                                         frag = tag_comment(frag);
    181                                 else
    182                                         if length(xmlstring)-frag.str>9 & strcmp(xmlstring(frag.str+1:frag.str+8),'![CDATA[')
    183                                                 %- Litteral data
    184                                                 frag = tag_cdata(frag);
    185                                         else
    186                                                 %- A tag element (empty (<.../>) or not)
    187                                                 if ~isempty(frag.end)
    188                                                         endmk = ['/' frag.end '>'];
    189                                                 else
    190                                                         endmk = '/>';
    191                                                 end
    192                                                 if strcmp(xmlstring(frag.str+1:frag.str+length(frag.end)+2),endmk) | ...
    193                                                         strcmp(strip(xmlstring(frag.str+1:frag.str+length(frag.end)+2)),endmk)
    194                                                         frag.str = frag.str + length(frag.end)+3;
    195                                                         return
    196                                                 else
    197                                                         frag = tag_element(frag);
    198                                                 end
    199                                         end
    200                                 end
    201                         end
    202                 end
    203         end
    204 
    205 %-----------------------------------------------------------------------
     147    global xmlstring xtree Xparse_count;
     148   
     149    while 1,
     150        if length(xmlstring)<=frag.str || ...
     151           (frag.str == length(xmlstring)-1 && strcmp(xmlstring(frag.str:end),' '))
     152            return
     153        end
     154        TagStart = xml_findstr(xmlstring,'<',frag.str,1);
     155        if isempty(TagStart)
     156            %- Character data
     157            error('[XML] Unknown data at the end of the XML file.');
     158            Xparse_count = Xparse_count + 1;
     159            xtree{Xparse_count} = chardata;
     160            xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:end)));
     161            xtree{Xparse_count}.parent = frag.parent;
     162            xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     163            frag.str = '';
     164        elseif TagStart > frag.str
     165            if strcmp(xmlstring(frag.str:TagStart-1),' ')
     166                %- A single white space before a tag (ignore)
     167                frag.str = TagStart;
     168            else
     169                %- Character data
     170                Xparse_count = Xparse_count + 1;
     171                xtree{Xparse_count} = chardata;
     172                xtree{Xparse_count}.value = erode(entity(xmlstring(frag.str:TagStart-1)));
     173                xtree{Xparse_count}.parent = frag.parent;
     174                xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     175                frag.str = TagStart;
     176            end
     177        else
     178            if strcmp(xmlstring(frag.str+1),'?')
     179                %- Processing instruction
     180                frag = tag_pi(frag);
     181            else
     182                if length(xmlstring)-frag.str>4 && strcmp(xmlstring(frag.str+1:frag.str+3),'!--')
     183                    %- Comment
     184                    frag = tag_comment(frag);
     185                else
     186                    if length(xmlstring)-frag.str>9 && strcmp(xmlstring(frag.str+1:frag.str+8),'![CDATA[')
     187                        %- Litteral data
     188                        frag = tag_cdata(frag);
     189                    else
     190                        %- A tag element (empty (<.../>) or not)
     191                        if ~isempty(frag.end)
     192                            endmk = ['/' frag.end '>'];
     193                        else
     194                            endmk = '/>';
     195                        end
     196                        if strcmp(xmlstring(frag.str+1:frag.str+length(frag.end)+2),endmk) || ...
     197                            strcmp(strip(xmlstring(frag.str+1:frag.str+length(frag.end)+2)),endmk)
     198                            frag.str = frag.str + length(frag.end)+3;
     199                            return
     200                        else
     201                            frag = tag_element(frag);
     202                        end
     203                    end
     204                end
     205            end
     206        end
     207    end
     208
     209%--------------------------------------------------------------------------
    206210function frag = tag_element(frag)
    207         global xmlstring xtree Xparse_count;
    208         close =  xml_findstr(xmlstring,'>',frag.str,1);
    209         if isempty(close)
    210                 error('[XML] Tag < opened but not closed.');
    211         else
    212                 empty = strcmp(xmlstring(close-1:close),'/>');
    213                 if empty
    214                         close = close - 1;
    215                 end
    216                 starttag = normalize(xmlstring(frag.str+1:close-1));
    217                 nextspace = xml_findstr(starttag,' ',1,1);
    218                 attribs = '';
    219                 if isempty(nextspace)
    220                         name = starttag;
    221                 else
    222                         name = starttag(1:nextspace-1);
    223                         attribs = starttag(nextspace+1:end);
    224                 end
    225                 xtree{Xparse_count} = element;
    226                 xtree{Xparse_count}.name = strip(name);
    227                 if frag.parent
    228                         xtree{Xparse_count}.parent = frag.parent;
    229                         xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    230                 end
    231                 if length(attribs) > 0
    232                         xtree{Xparse_count}.attributes = attribution(attribs);
    233                 end
    234                 if ~empty
    235                         contents = fragment;
    236                         contents.str = close+1;
    237                         contents.end = name;
    238                         contents.parent = Xparse_count;
    239                         contents = compile(contents);
    240                         frag.str = contents.str;
    241                 else
    242                         frag.str = close+2;
    243                 end
    244         end
    245 
    246 %-----------------------------------------------------------------------
     211    global xmlstring xtree Xparse_count;
     212    close =  xml_findstr(xmlstring,'>',frag.str,1);
     213    if isempty(close)
     214        error('[XML] Tag < opened but not closed.');
     215    else
     216        empty = strcmp(xmlstring(close-1:close),'/>');
     217        if empty
     218            close = close - 1;
     219        end
     220        starttag = normalize(xmlstring(frag.str+1:close-1));
     221        nextspace = xml_findstr(starttag,' ',1,1);
     222        attribs = '';
     223        if isempty(nextspace)
     224            name = starttag;
     225        else
     226            name = starttag(1:nextspace-1);
     227            attribs = starttag(nextspace+1:end);
     228        end
     229        Xparse_count = Xparse_count + 1;
     230        xtree{Xparse_count} = element;
     231        xtree{Xparse_count}.name = strip(name);
     232        if frag.parent
     233            xtree{Xparse_count}.parent = frag.parent;
     234            xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     235        end
     236        if ~isempty(attribs)
     237            xtree{Xparse_count}.attributes = attribution(attribs);
     238        end
     239        if ~empty
     240            contents = fragment;
     241            contents.str = close+1;
     242            contents.end = name;
     243            contents.parent = Xparse_count;
     244            contents = compile(contents);
     245            frag.str = contents.str;
     246        else
     247            frag.str = close+2;
     248        end
     249    end
     250
     251%--------------------------------------------------------------------------
    247252function frag = tag_pi(frag)
    248         global xmlstring xtree Xparse_count;
    249         close = xml_findstr(xmlstring,'?>',frag.str,1);
    250         if isempty(close)
    251                 warning('[XML] Tag <? opened but not closed.')
    252         else
    253                 nextspace = xml_findstr(xmlstring,' ',frag.str,1);
    254                 xtree{Xparse_count} = pri;
    255                 if nextspace > close | nextspace == frag.str+2
    256                         xtree{Xparse_count}.value = erode(xmlstring(frag.str+2:close-1));
    257                 else
    258                         xtree{Xparse_count}.value = erode(xmlstring(nextspace+1:close-1));
    259                         xtree{Xparse_count}.target = erode(xmlstring(frag.str+2:nextspace));
    260                 end
    261                 if frag.parent
    262                         xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    263                         xtree{Xparse_count}.parent = frag.parent;
    264                 end
    265                 frag.str = close+2;
    266         end
    267 
    268 %-----------------------------------------------------------------------
     253    global xmlstring xtree Xparse_count;
     254    close = xml_findstr(xmlstring,'?>',frag.str,1);
     255    if isempty(close)
     256        warning('[XML] Tag <? opened but not closed.')
     257    else
     258        nextspace = xml_findstr(xmlstring,' ',frag.str,1);
     259        Xparse_count = Xparse_count + 1;
     260        xtree{Xparse_count} = pri;
     261        if nextspace > close || nextspace == frag.str+2
     262            xtree{Xparse_count}.value = erode(xmlstring(frag.str+2:close-1));
     263        else
     264            xtree{Xparse_count}.value = erode(xmlstring(nextspace+1:close-1));
     265            xtree{Xparse_count}.target = erode(xmlstring(frag.str+2:nextspace));
     266        end
     267        if frag.parent
     268            xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     269            xtree{Xparse_count}.parent = frag.parent;
     270        end
     271        frag.str = close+2;
     272    end
     273
     274%--------------------------------------------------------------------------
    269275function frag = tag_comment(frag)
    270         global xmlstring xtree Xparse_count;
    271         close = xml_findstr(xmlstring,'-->',frag.str,1);
    272         if isempty(close)
    273                 warning('[XML] Tag <!-- opened but not closed.')
    274         else
    275                 xtree{Xparse_count} = comment;
    276                 xtree{Xparse_count}.value = erode(xmlstring(frag.str+4:close-1));
    277                 if frag.parent
    278                         xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    279                         xtree{Xparse_count}.parent = frag.parent;
    280                 end
    281                 frag.str = close+3;
    282         end
    283 
    284 %-----------------------------------------------------------------------
     276    global xmlstring xtree Xparse_count;
     277    close = xml_findstr(xmlstring,'-->',frag.str,1);
     278    if isempty(close)
     279        warning('[XML] Tag <!-- opened but not closed.')
     280    else
     281        Xparse_count = Xparse_count + 1;
     282        xtree{Xparse_count} = comment;
     283        xtree{Xparse_count}.value = erode(xmlstring(frag.str+4:close-1));
     284        if frag.parent
     285            xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     286            xtree{Xparse_count}.parent = frag.parent;
     287        end
     288        frag.str = close+3;
     289    end
     290
     291%--------------------------------------------------------------------------
    285292function frag = tag_cdata(frag)
    286         global xmlstring xtree Xparse_count;
    287         close = xml_findstr(xmlstring,']]>',frag.str,1);
    288         if isempty(close)
    289                 warning('[XML] Tag <![CDATA[ opened but not closed.')
    290         else
    291                 xtree{Xparse_count} = cdata;
    292                 xtree{Xparse_count}.value = xmlstring(frag.str+9:close-1);
    293                 if frag.parent
    294                         xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
    295                         xtree{Xparse_count}.parent = frag.parent;
    296                 end
    297                 frag.str = close+3;
    298         end
    299 
    300 %-----------------------------------------------------------------------
     293    global xmlstring xtree Xparse_count;
     294    close = xml_findstr(xmlstring,']]>',frag.str,1);
     295    if isempty(close)
     296        warning('[XML] Tag <![CDATA[ opened but not closed.')
     297    else
     298        Xparse_count = Xparse_count + 1;
     299        xtree{Xparse_count} = cdata;
     300        xtree{Xparse_count}.value = xmlstring(frag.str+9:close-1);
     301        if frag.parent
     302            xtree{frag.parent}.contents = [xtree{frag.parent}.contents Xparse_count];
     303            xtree{Xparse_count}.parent = frag.parent;
     304        end
     305        frag.str = close+3;
     306    end
     307
     308%--------------------------------------------------------------------------
    301309function all = attribution(str)
    302         %- Initialize attributs
    303         nbattr = 0;
    304         all = cell(nbattr);
    305         %- Look for 'key="value"' substrings
    306         while 1,
    307                 eq = xml_findstr(str,'=',1,1);
    308                 if isempty(str) | isempty(eq), return; end
    309                 id = xml_findstr(str,'"',1,1);       % should also look for ''''
    310                 nextid = xml_findstr(str,'"',id+1,1);% rather than only '"'
    311                 nbattr = nbattr + 1;
    312                 all{nbattr}.key = strip(str(1:(eq-1)));
    313                 all{nbattr}.val = entity(str((id+1):(nextid-1)));
    314                 str = str((nextid+1):end);
    315         end
    316 
    317 %-----------------------------------------------------------------------
     310    %- Initialize attributs
     311    nbattr = 0;
     312    all = cell(nbattr);
     313    %- Look for 'key="value"' substrings
     314    while 1,
     315        eq = xml_findstr(str,'=',1,1);
     316        if isempty(str) || isempty(eq), return; end
     317        id = sort([xml_findstr(str,'"',1,1),xml_findstr(str,'''',1,1)]); id=id(1);
     318        nextid = sort([xml_findstr(str,'"',id+1,1),xml_findstr(str,'''',id+1,1)]);nextid=nextid(1);
     319        nbattr = nbattr + 1;
     320        all{nbattr}.key = strip(str(1:(eq-1)));
     321        all{nbattr}.val = entity(str((id+1):(nextid-1)));
     322        str = str((nextid+1):end);
     323    end
     324
     325%--------------------------------------------------------------------------
    318326function elm = element
    319         global Xparse_count;
    320         Xparse_count = Xparse_count + 1;
    321         elm = struct('type','element','name','','attributes',[],'contents',[],'parent',[],'uid',Xparse_count);
     327    global Xparse_count;
     328    elm = struct('type','element','name','','attributes',[],'contents',[],'parent',[],'uid',Xparse_count);
    322329   
    323 %-----------------------------------------------------------------------
     330%--------------------------------------------------------------------------
    324331function cdat = chardata
    325         global Xparse_count;
    326         Xparse_count = Xparse_count + 1;
    327         cdat = struct('type','chardata','value','','parent',[],'uid',Xparse_count);
     332    global Xparse_count;
     333    cdat = struct('type','chardata','value','','parent',[],'uid',Xparse_count);
    328334   
    329 %-----------------------------------------------------------------------
     335%--------------------------------------------------------------------------
    330336function cdat = cdata
    331         global Xparse_count;
    332         Xparse_count = Xparse_count + 1;
    333         cdat = struct('type','cdata','value','','parent',[],'uid',Xparse_count);
     337    global Xparse_count;
     338    cdat = struct('type','cdata','value','','parent',[],'uid',Xparse_count);
    334339   
    335 %-----------------------------------------------------------------------
     340%--------------------------------------------------------------------------
    336341function proce = pri
    337         global Xparse_count;
    338         Xparse_count = Xparse_count + 1;
    339         proce = struct('type','pi','value','','target','','parent',[],'uid',Xparse_count);
    340 
    341 %-----------------------------------------------------------------------
     342    global Xparse_count;
     343    proce = struct('type','pi','value','','target','','parent',[],'uid',Xparse_count);
     344
     345%--------------------------------------------------------------------------
    342346function commt = comment
    343         global Xparse_count;
    344         Xparse_count = Xparse_count + 1;
    345         commt = struct('type','comment','value','','parent',[],'uid',Xparse_count);
    346 
    347 %-----------------------------------------------------------------------
     347    global Xparse_count;
     348    commt = struct('type','comment','value','','parent',[],'uid',Xparse_count);
     349
     350%--------------------------------------------------------------------------
    348351function frg = fragment
    349         frg = struct('str','','parent','','end','');
    350 
    351 %-----------------------------------------------------------------------
     352    frg = struct('str','','parent','','end','');
     353
     354%--------------------------------------------------------------------------
    352355function str = prolog(str)
    353         %- Initialize beginning index of elements tree
    354         b = 1;
    355         %- Initial tag
    356         start = xml_findstr(str,'<',1,1);
    357         if isempty(start)
    358                 error('[XML] No tag found.')
    359         end
    360         %- Header (<?xml version="1.0" ... ?>)
    361         if strcmp(lower(str(start:start+2)),'<?x')
    362                 close = xml_findstr(str,'?>',1,1);
    363                 if ~isempty(close)
    364                         b = close + 2;
    365                 else
    366                         warning('[XML] Header tag incomplete.')
    367                 end
    368         end
    369         %- Doctype (<!DOCTYPE type ... [ declarations ]>)
    370         start = xml_findstr(str,'<!DOCTYPE',b,1);  % length('<!DOCTYPE') = 9
    371         if ~isempty(start)
    372                 close = xml_findstr(str,'>',start+9,1);
    373                 if ~isempty(close)
    374                         b = close + 1;
    375                         dp = xml_findstr(str,'[',start+9,1);
    376                         if (~isempty(dp) & dp < b)
    377                                 k = xml_findstr(str,']>',start+9,1);
    378                                 if ~isempty(k)
    379                                         b = k + 2;
    380                                 else
    381                                         warning('[XML] Tag [ in DOCTYPE opened but not closed.')
    382                                 end
    383                         end
    384                 else
    385                         warning('[XML] Tag DOCTYPE opened but not closed.')
    386                 end
    387         end
    388         %- Skip prolog from the xml string
    389         str = str(b:end);
    390 
    391 %-----------------------------------------------------------------------
     356    %- Initialize beginning index of elements tree
     357    b = 1;
     358    %- Initial tag
     359    start = xml_findstr(str,'<',1,1);
     360    if isempty(start)
     361        error('[XML] No tag found.')
     362    end
     363    %- Header (<?xml version="1.0" ... ?>)
     364    if strcmpi(str(start:start+2),'<?x')
     365        close = xml_findstr(str,'?>',1,1);
     366        if ~isempty(close)
     367            b = close + 2;
     368        else
     369            warning('[XML] Header tag incomplete.')
     370        end
     371    end
     372    %- Doctype (<!DOCTYPE type ... [ declarations ]>)
     373    start = xml_findstr(str,'<!DOCTYPE',b,1);  % length('<!DOCTYPE') = 9
     374    if ~isempty(start)
     375        close = xml_findstr(str,'>',start+9,1);
     376        if ~isempty(close)
     377            b = close + 1;
     378            dp = xml_findstr(str,'[',start+9,1);
     379            if (~isempty(dp) && dp < b)
     380                k = xml_findstr(str,']>',start+9,1);
     381                if ~isempty(k)
     382                    b = k + 2;
     383                else
     384                    warning('[XML] Tag [ in DOCTYPE opened but not closed.')
     385                end
     386            end
     387        else
     388            warning('[XML] Tag DOCTYPE opened but not closed.')
     389        end
     390    end
     391    %- Skip prolog from the xml string
     392    str = str(b:end);
     393
     394%--------------------------------------------------------------------------
    392395function str = strip(str)
    393         a = isspace(str);
    394         a = find(a==1);
    395         str(a) = '';
    396 
    397 %-----------------------------------------------------------------------
     396    str(isspace(str)) = '';
     397
     398%--------------------------------------------------------------------------
    398399function str = normalize(str)
    399         % Find white characters (space, newline, carriage return, tabs, ...)
    400         i = isspace(str);
    401         i = find(i == 1);
    402         str(i) = ' ';
    403         % replace several white characters by only one
    404         if ~isempty(i)
    405                 j = i - [i(2:end) i(end)];
    406                 k = find(j == -1);
    407                 str(i(k)) = [];
    408         end
    409 
    410 %-----------------------------------------------------------------------
     400    % Find white characters (space, newline, carriage return, tabs, ...)
     401    i = isspace(str);
     402    i = find(i == 1);
     403    str(i) = ' ';
     404    % replace several white characters by only one
     405    if ~isempty(i)
     406        j = i - [i(2:end) i(end)];
     407        str(i(j == -1)) = [];
     408    end
     409
     410%--------------------------------------------------------------------------
    411411function str = entity(str)
    412         str = strrep(str,'&lt;','<');
    413         str = strrep(str,'&gt;','>');
    414         str = strrep(str,'&quot;','"');
    415         str = strrep(str,'&apos;','''');
    416         str = strrep(str,'&amp;','&');
     412    str = strrep(str,'&lt;','<');
     413    str = strrep(str,'&gt;','>');
     414    str = strrep(str,'&quot;','"');
     415    str = strrep(str,'&apos;','''');
     416    str = strrep(str,'&amp;','&');
    417417   
    418 %-----------------------------------------------------------------------
     418%--------------------------------------------------------------------------
    419419function str = erode(str)
    420         if ~isempty(str) & str(1)==' ' str(1)=''; end;
    421         if ~isempty(str) & str(end)==' ' str(end)=''; end;
     420    if ~isempty(str) && str(1)==' ', str(1)=''; end;
     421    if ~isempty(str) && str(end)==' ', str(end)=''; end;
  • trunk/src/@xmltree/root.m

    r723 r925  
    55% tree   - XMLTree object
    66% uid    - UID of the root element of tree
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Return the uid of the root element of the tree.
    10 %_______________________________________________________________________
    11 % @(#)root.m                   Guillaume Flandin               02/04/17
     10%__________________________________________________________________________
     11% Copyright (C) 2002-2008  http://www.artefact.tk/
     12
     13% Guillaume Flandin
     14% $Id: root.m 4460 2011-09-05 14:52:16Z guillaume $
    1215
    1316% Actually root is necessarily the element whos UID is 1, by
     
    2730% Look for the first element in the XML Tree
    2831for i=1:length(tree)
    29         if strcmp(get(tree,i,'type'),'element')
    30                 uid = i;
    31                 break
    32         end
     32    if strcmp(get(tree,i,'type'),'element')
     33        uid = i;
     34        break
     35    end
    3336end
  • trunk/src/@xmltree/save.m

    r821 r925  
    66% filename  - XML output filename
    77% varargout - XML string
    8 %_______________________________________________________________________
     8%__________________________________________________________________________
    99%
    1010% Convert an XML tree into a well-formed XML string and write it into
    1111% a file or return it as a string if no filename is provided.
    12 %_______________________________________________________________________
    13 % @(#)save.m                 Guillaume Flandin                 01/07/11
     12%__________________________________________________________________________
     13% Copyright (C) 2002-2011  http://www.artefact.tk/
    1414
    15 error(nargchk(1,2,nargin));
     15% Guillaume Flandin
     16% $Id: save.m 4460 2011-09-05 14:52:16Z guillaume $
     17
     18
     19%error(nargchk(1,2,nargin));
    1620
    1721prolog = '<?xml version="1.0" ?>\n';
     
    1923%- Return the XML tree as a string
    2024if nargin == 1
    21         varargout{1} = [sprintf(prolog) ...
    22                 print_subtree(tree,'',root(tree))];
     25    varargout{1} = [sprintf(prolog) ...
     26        print_subtree(tree,'',root(tree))];
    2327%- Output specified
    2428else
    25         %- Filename provided
    26         if isstr(filename)
    27                 [fid, msg] = fopen(filename,'w');
    28                 if fid==-1, error(msg); end
    29                 if isempty(tree.filename), tree.filename = filename; end
    30         %- File identifier provided
    31         elseif isnumeric(filename) & prod(size(filename)) == 1
    32                 fid = filename;
    33                 prolog = ''; %- With this option, do not write any prolog
    34         else
    35                 error('[XMLTree] Invalid argument.');
    36         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
     29    %- Filename provided
     30    if ischar(filename)
     31        [fid, msg] = fopen(filename,'w');
     32        if fid==-1, error(msg); end
     33        if isempty(tree.filename), tree.filename = filename; end
     34    %- File identifier provided
     35    elseif isnumeric(filename) && numel(filename) == 1
     36        fid = filename;
     37        prolog = ''; %- With this option, do not write any prolog
     38    else
     39        error('[XMLTree] Invalid argument.');
     40    end
     41    fprintf(fid,prolog);
     42    save_subtree(tree,fid,root(tree));
     43    if ischar(filename), fclose(fid); end
     44    if nargout == 1
     45        varargout{1} = print_subtree(tree,'',root(tree));
     46    end
    4347end
    4448
    45 %=======================================================================
     49%==========================================================================
    4650function 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
     51    if nargin < 4, order = 0; end
     52    xmlstr = [xmlstr blanks(3*order)];
     53    switch tree.tree{uid}.type
     54        case 'element'
     55            xmlstr = sprintf('%s<%s',xmlstr,tree.tree{uid}.name);
     56            for i=1:length(tree.tree{uid}.attributes)
     57                xmlstr = sprintf('%s %s="%s"', xmlstr, ...
     58                    tree.tree{uid}.attributes{i}.key,...
     59                    tree.tree{uid}.attributes{i}.val);
     60            end
     61            if isempty(tree.tree{uid}.contents)
     62                xmlstr = sprintf('%s/>\n',xmlstr);
     63            else
     64                xmlstr = sprintf('%s>\n',xmlstr);
     65                for i=1:length(tree.tree{uid}.contents)
     66                    xmlstr = print_subtree(tree,xmlstr, ...
     67                        tree.tree{uid}.contents(i),order+1);
     68                end
     69                xmlstr = [xmlstr blanks(3*order)];
     70                xmlstr = sprintf('%s</%s>\n',xmlstr,...
     71                    tree.tree{uid}.name);
     72            end
     73        case 'chardata'
     74            xmlstr = sprintf('%s%s\n',xmlstr, ...
     75                entity(tree.tree{uid}.value));
     76        case 'cdata'
     77            xmlstr = sprintf('%s<![CDATA[%s]]>\n',xmlstr, ...
     78                tree.tree{uid}.value);
     79        case 'pi'
     80            xmlstr = sprintf('%s<?%s %s?>\n',xmlstr, ...
     81                tree.tree{uid}.target, tree.tree{uid}.value);
     82        case 'comment'
     83            xmlstr = sprintf('%s<!-- %s -->\n',xmlstr,...
     84                tree.tree{uid}.value);
     85        otherwise
     86            warning(sprintf('Type %s unknown: not saved', ...
     87                tree.tree{uid}.type));
     88    end
    8589
    86 %=======================================================================
     90%==========================================================================
    8791function save_subtree(tree,fid,uid,order)
    88         if nargin < 4, order = 0; end
    89         fprintf(fid,blanks(3*order));
    90         switch tree.tree{uid}.type
    91                 case 'element'
    92                         fprintf(fid,'<%s',tree.tree{uid}.name);
    93                         for i=1:length(tree.tree{uid}.attributes)
    94                                 fprintf(fid,' %s="%s"',...
    95                                 tree.tree{uid}.attributes{i}.key, ...
    96                                 tree.tree{uid}.attributes{i}.val);
    97                         end
    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
    109                 case 'chardata'
    110                         fprintf(fid,'%s\n',entity(tree.tree{uid}.value));
    111                 case 'cdata'
    112                                 fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
    113                 case 'pi'
    114                         fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ...
    115                                 tree.tree{uid}.value);
    116                 case 'comment'
    117                         fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value);
    118                 otherwise
    119                         warning(sprintf('[XMLTree] Type %s unknown: not saved', ...
    120                                 tree.tree{uid}.type));
    121         end
     92    if nargin < 4, order = 0; end
     93    fprintf(fid,blanks(3*order));
     94    switch tree.tree{uid}.type
     95        case 'element'
     96            fprintf(fid,'<%s',tree.tree{uid}.name);
     97            for i=1:length(tree.tree{uid}.attributes)
     98                fprintf(fid,' %s="%s"',...
     99                tree.tree{uid}.attributes{i}.key, ...
     100                tree.tree{uid}.attributes{i}.val);
     101            end
     102            if isempty(tree.tree{uid}.contents)
     103                fprintf(fid,'/>\n');
     104            else
     105                fprintf(fid,'>\n');
     106                for i=1:length(tree.tree{uid}.contents)
     107                    save_subtree(tree,fid,...
     108                        tree.tree{uid}.contents(i),order+1)
     109                end
     110                fprintf(fid,blanks(3*order));
     111                fprintf(fid,'</%s>\n',tree.tree{uid}.name);
     112            end
     113        case 'chardata'
     114            fprintf(fid,'%s\n',entity(tree.tree{uid}.value));
     115        case 'cdata'
     116                fprintf(fid,'<![CDATA[%s]]>\n',tree.tree{uid}.value);
     117        case 'pi'
     118            fprintf(fid,'<?%s %s?>\n',tree.tree{uid}.target, ...
     119                tree.tree{uid}.value);
     120        case 'comment'
     121            fprintf(fid,'<!-- %s -->\n',tree.tree{uid}.value);
     122        otherwise
     123            warning(sprintf('[XMLTree] Type %s unknown: not saved', ...
     124                tree.tree{uid}.type));
     125    end
    122126
    123127
    124 %=======================================================================
     128%==========================================================================
    125129function str = entity(str)
    126         str = strrep(str,'&','&amp;');
    127         str = strrep(str,'<','&lt;');
    128         str = strrep(str,'>','&gt;');
    129         str = strrep(str,'"','&quot;');
    130         str = strrep(str,'''','&apos;');
     130    % This has the side effect of strtrim'ming the char array.
     131    str = char(strrep(cellstr(str), '&',  '&amp;' ));
     132    str = char(strrep(cellstr(str), '<',  '&lt;'  ));
     133    str = char(strrep(cellstr(str), '>',  '&gt;'  ));
     134    str = char(strrep(cellstr(str), '"',  '&quot;'));
     135    str = char(strrep(cellstr(str), '''', '&apos;'));
  • trunk/src/@xmltree/set.m

    r723 r925  
    77% parameter - property name
    88% value     - property value
    9 %_______________________________________________________________________
     9%__________________________________________________________________________
    1010%
    1111% Set object properties given its uid and pairs parameter/value
    1212% The tree parameter must be in input AND in output
    13 %_______________________________________________________________________
    14 % @(#)set.m                   Guillaume Flandin                02/03/27
     13%__________________________________________________________________________
     14% Copyright (C) 2002-2011  http://www.artefact.tk/
    1515
    16 error(nargchk(4,4,nargin));
     16% Guillaume Flandin
     17% $Id: set.m 4460 2011-09-05 14:52:16Z guillaume $
     18
     19
     20%error(nargchk(4,4,nargin));
    1721
    1822if iscell(uid), uid = [uid{:}]; else uid = uid(:); end
    1923
    2024for i=1:length(uid)
    21         tree.tree{uid(i)} = builtin('subsasgn', tree.tree{uid(i)}, struct('type','.','subs',parameter), value);
    22         %tree.tree{uid(i)} = setfield(tree.tree{uid(i)},parameter,value);
     25    tree.tree{uid(i)} = builtin('subsasgn', tree.tree{uid(i)}, struct('type','.','subs',parameter), value);
     26    %tree.tree{uid(i)} = setfield(tree.tree{uid(i)},parameter,value);
    2327end
  • trunk/src/@xmltree/setfilename.m

    r723 r925  
    55% tree     - XMLTree object
    66% filename - XML filename
    7 %_______________________________________________________________________
     7%__________________________________________________________________________
    88%
    99% Set the filename linked to the XML tree as filename.
    10 %_______________________________________________________________________
    11 % @(#)setfilename.m               Guillaume Flandin            02/03/27
     10%__________________________________________________________________________
     11% Copyright (C) 2002-2011  http://www.artefact.tk/
     12
     13% Guillaume Flandin
     14% $Id: setfilename.m 4460 2011-09-05 14:52:16Z guillaume $
    1215
    1316tree.filename = filename;
  • trunk/src/@xmltree/xmltree.m

    r821 r925  
    99%     tree = xmltree('foo.xml');  % creates a tree from XML file 'foo.xml'
    1010%     tree = xmltree('<tag>content</tag>') % creates a tree from string
    11 %_______________________________________________________________________
     11%__________________________________________________________________________
    1212%
    1313% This is the constructor of the XMLTree class.
     
    1616% See http://www.w3.org/TR/REC-xml for details about XML 1.0.
    1717% See http://www.w3.org/DOM/ for details about DOM platform.
    18 %_______________________________________________________________________
    19 % @(#)xmltree.m                 Guillaume Flandin              02/03/27
     18%__________________________________________________________________________
     19% Copyright (C) 2002-2011  http://www.artefact.tk/
     20
     21% Guillaume Flandin
     22% $Id: xmltree.m 4460 2011-09-05 14:52:16Z guillaume $
    2023
    2124switch(nargin)
    22         case 0
    23                 tree.tree{1} = struct('type','element',...
    24                                       'name','tag',...
    25                                                       'attributes',[],...
    26                                                       'contents',[],...
    27                                                           'parent',[],...
    28                                                       'uid',1);
    29                 tree.filename = '';
    30                 tree = class(tree,'xmltree');
    31         case 1
    32                 if isa(varargin{1},'xmltree')
    33                         tree = varargin{1};
    34                 elseif ischar(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
    51                         tree = class(tree,'xmltree');
    52                 else
    53                         error('[XMLTree] Bad input argument');
    54                 end
    55         otherwise
    56                 error('[XMLTree] Too many input arguments');
     25    case 0
     26        tree.tree{1} = struct('type','element',...
     27                              'name','tag',...
     28                              'attributes',[],...
     29                              'contents',[],...
     30                              'parent',[],...
     31                              'uid',1);
     32        tree.filename = '';
     33        tree = class(tree,'xmltree');
     34    case 1
     35        if isa(varargin{1},'xmltree')
     36            tree = varargin{1};
     37        elseif ischar(varargin{1})
     38            % Input argument is an XML string
     39            if (~exist(varargin{1},'file') && ...
     40                ~isempty(xml_findstr(varargin{1},'<',1,1)))
     41                tree.tree = xml_parser(varargin{1});
     42                tree.filename = '';
     43            % Input argument is an XML filename
     44            else
     45                fid = fopen(varargin{1},'rt');
     46                if (fid == -1)
     47                    error(['[XMLTree] Cannot open ' varargin{1}]);
     48                end
     49                xmlstr = fread(fid,'*char')';
     50                %xmlstr = fscanf(fid,'%c');
     51                fclose(fid);
     52                tree.tree = xml_parser(xmlstr);
     53                tree.filename = varargin{1};
     54            end
     55            tree = class(tree,'xmltree');
     56        else
     57            error('[XMLTree] Bad input argument');
     58        end
     59    otherwise
     60        error('[XMLTree] Too many input arguments');
    5761end
Note: See TracChangeset for help on using the changeset viewer.