This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_xml_file.m
166 lines (135 loc) · 5.97 KB
/
read_xml_file.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%read values from xml files with direct output (easier than xmlreal);
% made first for retrieving motor position if absent from radio header;
% more options to come (maybe);
%
% usage: [ value ] = read_xml_file(filename,xmlnode,what)
%
% - filename: string of character, full path of xml file to read
%
% - xmlnode: node to look in within the xml file. example: motor\
%
% - what: type of information requested. Example: within the motor
% nodes, two child nodes are motorName and motorPosition;
% requesting what='sz' as motorName label will return motorPosition
% of sz
%
% example:
%
% [motor_position_value]=read_xml_file('myXmlFile.xml','motor','sz')
function [ value ] = read_xml_file(filename,xmlnode,what,varargin)
switch nargin
case 4
ConvertValue=varargin{1};
otherwise
ConvertValue='';
end
infoLabel = what; infoCbk = ''; itemFound = false;textdisp='';childText='';value=[];
xDoc = xmlread(filename);
switch xmlnode
case 'motor'
AllMotors= xDoc.getElementsByTagName(xmlnode);
NbOfMotors=AllMotors.getLength;
for i=0:AllMotors.getLength-1
thisListItem = AllMotors.item(i);
childNode = thisListItem.getFirstChild;
while ~isempty(childNode)
%Filter out text, comments, and processing instructions.
if childNode.getNodeType == childNode.ELEMENT_NODE
try
%Assume that each element has a single org.w3c.dom.Text child
childText = char(childNode.getFirstChild.getData);
textdisp=char(childNode.getTagName);
switch char(childNode.getTagName)
case 'motorName' ; itemFound = strcmp(childText,infoLabel);
case 'motorPosition' ; infoCbk = childText;
end
catch
childText='Empty';
end
end
childNode = childNode.getNextSibling;
%fprintf('round %1.0f checking: %s %s %s \n',i,textdisp, childText,infoCbk);
end
if itemFound break; else infoCbk = ''; end
end
value=str2num(infoCbk);
case 'acquisition'
AllItems= xDoc.getElementsByTagName(xmlnode);
NbOfItems=AllItems.getLength;
for i=0:AllItems.getLength-1
thisListItem = AllItems.item(i);
childNode = thisListItem.getFirstChild;
while ~isempty(childNode)
%Filter out text, comments, and processing instructions.
if childNode.getNodeType == childNode.ELEMENT_NODE
%Assume that each element has a single org.w3c.dom.Text child
try
childText = char(childNode.getFirstChild.getData);
char(childNode.getTagName);
switch char(childNode.getTagName)
case infoLabel ; itemFound = 1; infoCbk=childText;
end
catch
childText='Empty';
end
end
childNode = childNode.getNextSibling;
%fprintf('round %1.0f checking: %s %s %s \n',i,textdisp, childText,infoCbk);
end
if itemFound break; else infoCbk = ''; end
end
value=infoCbk;
case 'projectionSize'
AllItems= xDoc.getElementsByTagName(xmlnode);
NbOfItems=AllItems.getLength;
for i=0:AllItems.getLength-1
thisListItem = AllItems.item(i);
childNode = thisListItem.getFirstChild;
while ~isempty(childNode)
%Filter out text, comments, and processing instructions.
if childNode.getNodeType == childNode.ELEMENT_NODE
try
%Assume that each element has a single org.w3c.dom.Text child
childText = char(childNode.getFirstChild.getData);
char(childNode.getTagName);
switch char(childNode.getTagName)
case infoLabel ; itemFound = 1; infoCbk=childText;
end
catch
childText='Empty';
end
end
childNode = childNode.getNextSibling;
%fprintf('round %1.0f checking: %s %s %s \n',i,textdisp, childText,infoCbk);
end
if itemFound break; else infoCbk = ''; end
end
value=infoCbk;
otherwise
AllItems= xDoc.getElementsByTagName(xmlnode);
NbOfItems=AllItems.getLength;
for i=0:AllItems.getLength-1
thisListItem = AllItems.item(i);
childNode = thisListItem.getFirstChild;
while ~isempty(childNode)
%Filter out text, comments, and processing instructions.
if childNode.getNodeType == childNode.ELEMENT_NODE
%Assume that each element has a single org.w3c.dom.Text child
childText = char(childNode.getFirstChild.getData);
char(childNode.getTagName);
switch char(childNode.getTagName)
case infoLabel ; itemFound = 1; infoCbk=childText;
end
end
childNode = childNode.getNextSibling;
%fprintf('round %1.0f checking: %s %s %s \n',i,textdisp, childText,infoCbk);
end
if itemFound break; else infoCbk = ''; end
end
value=infoCbk;
switch ConvertValue
case 'numeric'
value=str2num(value) ;
end
end %end of switch
end %end of function