Debian Codegraph @

matlab/+ismrmrd/+xml/deserialize.m

raw
1 function [header] = deserialize(xmlstring)
2 %DESERIALIZE Summary of this function goes here
3 % Detailed explanation goes here
4
5 % Get a parser
6 db = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
7
8 % Turn the string into a stream
9 isrc = org.xml.sax.InputSource();
10 isrc.setCharacterStream(java.io.StringReader(xmlstring))
11
12 % Parse it
13 dom = db.parse(isrc);
14
15 % Get the root element
16 rootNode = dom.getDocumentElement();
17
18 % Fill it
19 header = parseNode(rootNode);
20
21 end
22
23 % ----- Subfunction parseChild -----
24 function info = parseNode(theNode)
25
26 % Walk down the tree
27 childNodes = getChildNodes(theNode);
28 numChildNodes = getLength(childNodes);
29
30 info = struct;
31
32 for n = 1:numChildNodes
33 theChild = item(childNodes,n-1);
34 name = char(getNodeName(theChild));
35
36 %Some elements occur more than once
37 if isfield(info, name)
38 % Use cell arrays for multiple valued strings and dates
39 if (isStringType(name) || isDateType(name)) && ~iscell(info.(name))
40 info.(name) = {info.(name)};
41 end
42
43 num = length(info.(name))+1;
44 else
45 num = 1;
46 end
47
48 if strcmp(name, 'encoding')
49 if num == 1
50 info.encoding = struct('encodedSpace',struct,'reconSpace',struct, ...
51 'encodingLimits', struct, 'trajectory', '', ...
52 'trajectoryDescription', struct, ...
53 'parallelImaging', struct, 'echoTrainLength', []);
54 end
55 temp = parseNode(theChild);
56 fnames = fieldnames(temp);
57 for f = 1:length(fnames)
58 info.encoding(num).(fnames{f}) = temp.(fnames{f});
59 end
60 continue;
61 end
62
63 % kspace_encoding_step_1/2 can be either part of the acceleration
64 % factor or part of the encoding limits.
65 if (strcmp(name, 'kspace_encoding_step_1') || strcmp(name, 'kspace_encoding_step_2'))
66 if strcmp(char(theChild.getParentNode.getNodeName),'encodingLimits')
67 info.(name) = parseNode(theChild);
68 else
69 info.(name) = str2num(getTextContent(theChild));
70 end
71 continue;
72 end
73
74 if isCompoundType(name)
75 if num == 1
76 info.(name) = parseNode(theChild);
77 else
78 info.(name)(num) = parseNode(theChild);
79 end
80 continue;
81 end
82
83 if isUserParameterType(name)
84 if num == 1
85 info.(name) = parseUserParameter(theChild);
86 else
87 info.(name)(num) = parseUserParameter(theChild);
88 end
89 continue;
90 end
91
92 if isStringType(name)
93 if num == 1
94 info.(name) = char(getTextContent(theChild));
95 else
96 info.(name){num} = char(getTextContent(theChild));
97 end
98 continue;
99 end
100
101 if isNumericalType(name)
102 if num == 1
103 info.(name) = str2num(getTextContent(theChild));
104 else
105 info.(name)(num) = str2num(getTextContent(theChild));
106 end
107 continue;
108 end
109
110 if isDateType(name)
111 if num == 1
112 info.(name) = char(getTextContent(theChild));
113 else
114 info.(name){num} = char(getTextContent(theChild));
115 end
116 continue;
117 end
118
119 end
120
121 end
122
123 %%%%%%%%%%%%%%%%%%%
124 function info = parseUserParameter(theNode)
125
126 paramType = char(getNodeName(theNode));
127 childNodes = getChildNodes(theNode);
128 numChildNodes = getLength(childNodes);
129
130 info = struct;
131 info.value = [];
132
133 for n = 1:numChildNodes
134 theChild = item(childNodes,n-1);
135 if strcmp(getNodeName(theChild),'name')
136 info.name = char(getTextContent(theChild));
137 end
138 if strcmp(getNodeName(theChild),'value')
139 if strcmp(paramType, 'userParameterLong') || strcmp(paramType, 'userParameterDouble')
140 info.value(end+1) = str2num(getTextContent(theChild));
141 end
142
143 if strcmp(paramType, 'userParameterString') || strcmp(paramType, 'userParameterBase64')
144 info.value = char(getTextContent(theChild));
145 end
146 end
147 end
148 end
149
150 % ----- Type specific functions ----
151 function status = isCompoundType(name)
152
153 % treat encoding separately
154 headerNodeNames = { ...
155 'subjectInformation', ...
156 'studyInformation', ...
157 'measurementInformation', ...
158 'acquisitionSystemInformation', ...
159 'experimentalConditions', ...
160 'coilLabel', ...
161 'encoding', ...
162 'sequenceParameters', ...
163 'userParameters', ...
164 'measurementDependency', ...
165 'referencedImageSequence', ...
166 'encodedSpace', ...
167 'reconSpace', ...
168 'encodingLimits', ...
169 'trajectoryDescription', ...
170 'parallelImaging', ...
171 'accelerationFactor', ...
172 'matrixSize', ...
173 'fieldOfView_mm', ...
174 'relativeTablePosition', ...
175 'kspace_encoding_step_0', ...
176 'kspace_encoding_step_1', ...
177 'kspace_encoding_step_2', ...
178 'average', ...
179 'slice', ...
180 'contrast', ...
181 'phase', ...
182 'repetition', ...
183 'set', ...
184 'segment', ...
185 'waveformInformation'
186 };
187
188 status = ismember(name, headerNodeNames);
189 end
190
191 function status = isNumericalType(name)
192 headerNumericalTypes = { ...
193 'version', ...
194 'patientWeight_kg', ...
195 'patientHeight_m', ...
196 'accessionNumber', ...
197 'initialSeriesNumber', ...
198 'systemFieldStrength_T', ...
199 'relativeReceiverNoiseBandwidth', ...
200 'receiverChannels', 'coilNumber', ...
201 'H1resonanceFrequency_Hz', ...
202 'TR', ...
203 'TE', ...
204 'TI', ...
205 'flipAngle_deg', ...
206 'sequence_type', ...
207 'echo_spacing', ...
208 'echoTrainLength', ...
209 'x', 'y', 'z', ...
210 'minimum', 'maximum', 'center'};
211
212 status = ismember(name, headerNumericalTypes);
213 end
214
215 function status = isStringType(name)
216 headerStringTypes = {...
217 'patientName', ...
218 'patientID', ...
219 'patientGender', ...
220 'studyID', ...
221 'referringPhysicianName', ...
222 'studyDescription', ...
223 'studyInstanceUID', ...
224 'measurementID', ...
225 'patientPosition', ...
226 'protocolName', ...
227 'seriesDescription', ...
228 'seriesInstanceUIDRoot', ...
229 'frameOfReferenceUID', ...
230 'referencedSOPInstanceUID', ...
231 'dependencyType', ...
232 'measurementID', ...
233 'systemVendor', ...
234 'systemModel', ...
235 'institutionName', ...
236 'stationName', ...
237 'deviceID', ...
238 'trajectory', ...
239 'identifier', ...
240 'comment', ...
241 'coilName', ...
242 'calibrationMode',...
243 'interleavingDimension',...
244 'sequence_type',...
245 'waveformName',...
246 'waveformType'
247 };
248
249 status = ismember(name, headerStringTypes);
250 end
251
252 function status = isDateType(name)
253 headerDateTypes = {...
254 'patientBirthdate', ...
255 'studyDate', ...
256 'studyTime', ...
257 'seriesDate', ...
258 'seriesTime'};
259 status = ismember(name, headerDateTypes);
260 end
261
262 function status = isUserParameterType(name)
263 typeNames = { ...
264 'userParameterLong', ...
265 'userParameterDouble', ...
266 'userParameterString', ....
267 'userParameterBase64'};
268
269 status = ismember(name, typeNames);
270 end