File: struct_2_xmlNode.m

package info (click to toggle)
openems 0.0.35%2Bgit20190103.6a75e98%2Bdfsg.1-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,544 kB
  • sloc: cpp: 40,417; python: 2,028; yacc: 580; makefile: 459; lex: 350; sh: 176; ruby: 19
file content (48 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function docElem = struct_2_xmlNode(docNode, docElem, mat_struct) 

if (isfield(mat_struct,'ATTRIBUTE'))
    names = fieldnames(mat_struct.ATTRIBUTE);
    for n=1:numel(names)
        if isnumeric(getfield(mat_struct.ATTRIBUTE,names{n})) || islogical(getfield(mat_struct.ATTRIBUTE,names{n}))
            docElem.setAttribute(names{n},vector2str(getfield(mat_struct.ATTRIBUTE,names{n}),15));
        else
            docElem.setAttribute(names{n},getfield(mat_struct.ATTRIBUTE,names{n}));
        end
    end
    mat_struct = rmfield(mat_struct,'ATTRIBUTE');
end

names = fieldnames(mat_struct);

for n=1:numel(names)
    if isstruct(getfield(mat_struct,names{n}))
        docNewElem = docNode.createElement(names{n});
        docNewElem = struct_2_xmlNode(docNode, docNewElem, getfield(mat_struct,names{n}));
        docElem.appendChild(docNewElem);
    elseif iscell(getfield(mat_struct,names{n}))
        cellfield = getfield(mat_struct,names{n});
        for m=1:numel(cellfield)
            if ischar(cellfield{m})
                docNewElem = docNode.createElement(names{n});
                docNewElem.appendChild(docNode.createTextNode(cellfield{m}));
                docElem.appendChild(docNewElem);
            else
                docNewElem = docNode.createElement(names{n});
                docNewElem = struct_2_xmlNode(docNode, docNewElem, cellfield{m});
                docElem.appendChild(docNewElem);
            end
        end
    elseif isempty(getfield(mat_struct,names{n}))
        %do nothing...
    elseif isnumeric(getfield(mat_struct,names{n}))
        number = getfield(mat_struct,names{n});
        str = vector2str(number,15);
        docNewElem = docNode.createElement(names{n});
        docNewElem.appendChild(docNode.createTextNode(str));
        docElem.appendChild(docNewElem);
    elseif ischar(getfield(mat_struct,names{n}))
        docNewElem = docNode.createElement(names{n});
        docNewElem.appendChild(docNode.createTextNode(getfield(mat_struct,names{n})));
        docElem.appendChild(docNewElem);
    end
end