source: trunk/src/@xmltree/branch.m @ 867

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

xmltree_updated

File size: 1.6 KB
Line 
1function subtree = branch(tree,uid)
2% XMLTREE/BRANCH Branch Method
3% FORMAT uid = parent(tree,uid)
4%
5% tree    - XMLTree object
6% uid     - UID of the root element of the subtree
7% subtree - XMLTree object (a subtree from tree)
8%_______________________________________________________________________
9%
10% Return a subtree from a tree.
11%_______________________________________________________________________
12% @(#)branch.m                  Guillaume Flandin              02/04/17
13
14error(nargchk(2,2,nargin));
15
16if uid > length(tree) | ...
17   prod(size(uid))~=1 | ...
18   ~strcmp(tree.tree{uid}.type,'element')
19        error('[XMLTree] Invalid UID.');
20end
21
22subtree = xmltree;
23subtree = set(subtree,root(subtree),'name',tree.tree{uid}.name);
24%- fix by Piotr Dollar to copy attributes for the root node:
25subtree = set(subtree,root(subtree),'attributes',tree.tree{uid}.attributes);
26
27child = children(tree,uid);
28
29for 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];
33end
34
35%=======================================================================
36function tree = sub_branch(t,tree,uid,p)
37
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
Note: See TracBrowser for help on using the repository browser.