-
Notifications
You must be signed in to change notification settings - Fork 1
/
ismrmrd_xml.m
147 lines (127 loc) · 4.44 KB
/
ismrmrd_xml.m
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function header = ismrmrd_xml(fname, schema)
% FORMAT header = ismrmrd_xml(fname)
%
% Read the flexible header of an ismrmrd file into a structure
% -------------------------------------------------------------------------
% Read schema
if nargin < 2
schema = '';
end
if ~isstruct(schema)
schema = ismrmrd_schema(schema);
end
% -------------------------------------------------------------------------
% Read header
if ischar(fname)
[~,~,ext] = fileparts(fname);
switch ext
case '.h5'
xmlstring = h5read(fname, '/dataset/xml');
xmlstring = xmlstring{1};
DOMnode = xmlreadstring(xmlstring);
case '.xml'
DOMnode = xmlread(fname);
otherwise
DOMnode = xmlreadstring(fname);
end
elseif isa(fname, 'org.apache.xerces.dom.DeferredDocumentImpl')
DOMnode = fname;
end
% -------------------------------------------------------------------------
% Parse header
header = parseNode(DOMnode, schema);
% =========================================================================
function obj = parseNode(DOMnode, schema)
% Recurse over node children.
nodeName = char(DOMnode.getNodeName);
if isfield(schema, 'parse') && ~isempty(schema.parse)
% ---------------------------------------------------------------------
% Parsable value (= leaf)
obj = [];
childNodes = DOMnode.getChildNodes;
numChildNodes = childNodes.getLength;
for c=1:numChildNodes
theChild = childNodes.item(c-1);
childName = char(theChild.getNodeName);
if childName(1) == '#'
if ~isempty(char(theChild.getData))
switch childName(2:end)
case 'text'
value = schema.parse(theChild.getData);
if isnumeric(value)
obj(end+1) = value;
else
if isempty(obj)
obj = {value};
else
obj{end+1} = value;
end
end
otherwise
warning('Unknown type %s',childName)
end
end
continue
end
end
if isfield(schema, 'minOccurs')
if isfield(schema, 'default')
value = schema.parse(schema.default);
for i=1:(schema.minOccurs-numel(obj))
if isnumeric(value)
obj(end+1) = value;
else
if isempty(obj)
obj = {value};
else
obj{end+1} = value;
end
end
end
end
if numel(obj) < schema.minOccurs
warning('Not enough values');
end
end
if isfield(schema, 'maxOccurs')
if numel(obj) > schema.maxOccurs
warning('Too many values for %s (%d/%d)', nodeName, numel(obj), schema.maxOccurs);
if iscell(obj)
obj = obj{1:schema.maxOccurs};
else
obj = obj(1:schema.maxOccurs);
end
end
end
if numel(obj) == 1 && iscell(obj)
obj = obj{1};
end
else
% ---------------------------------------------------------------------
% Non-parsable value (= node)
obj = struct;
protected = {'type' 'parse' 'default' 'maxOccurs' 'minOccurs' ...
'enumeration' 'fractionDigits' 'length' ...
'maxExclusive' 'maxInclusive' 'maxLength' ...
'minExclusive' 'minInclusive' 'minLength' ...
'pattern' 'totalDigits'};
fields = fieldnames(schema);
childNodes = DOMnode.getChildNodes;
numChildNodes = childNodes.getLength;
for c=1:numChildNodes
theChild = childNodes.item(c-1);
childName = char(theChild.getNodeName);
if any(strcmpi(childName, fields)) && ...
~any(strcmpi(childName, protected))
child = parseNode(theChild, schema.(childName));
if ~isfield(obj, childName)
obj.(childName) = child;
else
if ~iscell(obj.(childName))
obj.(childName) = {obj.(childName)};
end
obj.(childName){end+1} = child;
end
end
end
end