source: trunk/src/@xmltree/add.m @ 945

Last change on this file since 945 was 925, checked in by sommeria, 8 years ago

xmltree updated

File size: 3.6 KB
Line 
1function varargout = add(tree,uid,type,parameter)
2% XMLTREE/ADD Method (add childs to elements of an XML Tree)
3% FORMAT vararout = add(tree,uid,type,parameter)
4%
5% tree      - XMLTree object
6% uid       - array of uid's
7% type      - 'element', 'chardata', 'cdata', 'pi' or 'comment'
8% parameter - property name (a character array unless type='pi' for
9%             which parameter=struct('target','','value',''))
10%
11% new_uid   - UID's of the newly created nodes
12%
13%        tree = add(tree,uid,type,parameter);
14%        [tree, new_uid] = add(tree,uid,type,parameter);
15%__________________________________________________________________________
16%
17% Add a node (element, chardata, cdata, pi or comment) in the XML Tree.
18% It adds a child to the element whose UID is iud.
19% Use attributes({'set','get','add','del','length'},...) function to
20% deal with the attributes of an element node (initialized empty).
21% The tree parameter must be in input AND in output.
22%__________________________________________________________________________
23% Copyright (C) 2002-2011  http://www.artefact.tk/
24
25% Guillaume Flandin
26% $Id: add.m 4460 2011-09-05 14:52:16Z guillaume $
27
28
29if ~isa(uid,'double')
30    error('[XMLTree] UID must be a double array.');
31end
32if ~ischar(type)
33    error('[XMLTree] TYPE must be a valid item type.');
34end
35if strcmp(type,'pi')
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
41elseif ~ischar(parameter)
42    error('[XMLTree] PARAMETER must be a string.');
43end
44
45if nargout == 2
46    l = length(tree.tree);
47    varargout{2} = (l+1):(l+numel(uid));
48end
49
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);
92end
93
94varargout{1} = tree;
Note: See TracBrowser for help on using the repository browser.