[723] | 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 | |
---|
| 25 | child = children(tree,uid); |
---|
| 26 | |
---|
| 27 | for i=1:length(child) |
---|
| 28 | l = length(subtree); |
---|
| 29 | subtree = sub_branch(tree,subtree,child(i),root(subtree)); |
---|
| 30 | subtree.tree{root(subtree)}.contents = [subtree.tree{root(subtree)}.contents l+1]; |
---|
| 31 | end |
---|
| 32 | |
---|
| 33 | %======================================================================= |
---|
| 34 | function tree = sub_branch(t,tree,uid,p) |
---|
| 35 | |
---|
| 36 | l = length(tree); |
---|
| 37 | tree.tree{l+1} = t.tree{uid}; |
---|
| 38 | tree.tree{l+1}.uid = l + 1; |
---|
| 39 | tree.tree{l+1}.parent = p; |
---|
| 40 | tree.tree{l+1}.contents = []; |
---|
| 41 | if isfield(t.tree{uid},'contents') |
---|
| 42 | contents = get(t,uid,'contents'); |
---|
| 43 | m = length(tree); |
---|
| 44 | for i=1:length(contents) |
---|
| 45 | tree.tree{l+1}.contents = [tree.tree{l+1}.contents m+1]; |
---|
| 46 | tree = sub_branch(t,tree,contents(i),l+1); |
---|
| 47 | m = length(tree); |
---|
| 48 | end |
---|
| 49 | end |
---|