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

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

xmltree updated

File size: 1.8 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% Copyright (C) 2002-2011  http://www.artefact.tk/
13
14% Guillaume Flandin
15% $Id: branch.m 4460 2011-09-05 14:52:16Z guillaume $
16
17
18%error(nargchk(2,2,nargin));
19
20if uid > length(tree) || ...
21   numel(uid)~=1 || ...
22   ~strcmp(tree.tree{uid}.type,'element')
23    error('[XMLTree] Invalid UID.');
24end
25
26subtree = xmltree;
27subtree = set(subtree,root(subtree),'name',tree.tree{uid}.name);
28%- fix by Piotr Dollar to copy attributes for the root node:
29subtree = set(subtree,root(subtree),'attributes',tree.tree{uid}.attributes);
30
31child = children(tree,uid);
32
33for i=1:length(child)
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];
37end
38
39%==========================================================================
40function tree = sub_branch(t,tree,uid,p)
41
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
Note: See TracBrowser for help on using the repository browser.