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

Last change on this file since 723 was 723, checked in by sommeria, 10 years ago

xmltree and toolbox_calib added to svn

File size: 3.0 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% @(#)add.m                   Guillaume Flandin                02/03/29
24
25error(nargchk(4,4,nargin));
26
27if ~isa(uid,'double')
28        error('[XMLTree] UID must be a double array.');
29end
30if ~ischar(type)
31        error('[XMLTree] TYPE must be a valid item type.');
32end
33if 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
39elseif ~ischar(parameter)
40        error('[XMLTree] PARAMETER must be a string.');
41end
42
43if nargout == 2
44        l = length(tree.tree);
45        varargout{2} = (l+1):(l+prod(size(uid)));
46end
47
48for 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);
90end
91
92varargout{1} = tree;
Note: See TracBrowser for help on using the repository browser.