1 | function 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 | |
---|
14 | error(nargchk(2,2,nargin)); |
---|
15 | |
---|
16 | if uid > length(tree) | ... |
---|
17 | prod(size(uid))~=1 | ... |
---|
18 | ~strcmp(tree.tree{uid}.type,'element') |
---|
19 | error('[XMLTree] Invalid UID.'); |
---|
20 | end |
---|
21 | |
---|
22 | subtree = xmltree; |
---|
23 | subtree = set(subtree,root(subtree),'name',tree.tree{uid}.name); |
---|
24 | %- fix by Piotr Dollar to copy attributes for the root node: |
---|
25 | subtree = set(subtree,root(subtree),'attributes',tree.tree{uid}.attributes); |
---|
26 | |
---|
27 | child = children(tree,uid); |
---|
28 | |
---|
29 | for 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]; |
---|
33 | end |
---|
34 | |
---|
35 | %======================================================================= |
---|
36 | function 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 |
---|