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

xmltree updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.