How to read XML files
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a strange XML file. I am not familiar with this format. I need some help at reading the values contained in it. I am attaching an example file. I am interested in the couple of values separated by ",". For instance: d1,v1; d2,v2; ..... ; d_n,v_n I would like to get a vector with all d_i values and another vector with all v_i numbers. I need some help. Example: version="1.0"?
-<BeamDataDocument Version="1.0">
-<BeamData DataTypeId="OPD">
<Curve Depth="1.1" FieldSize="350.0"
0 Kommentare
Antworten (1)
SAI SRUJAN
am 8 Jul. 2024
Hi Maura,
I understand that you are facing an issue reading the xml file and parsing the data from the 'BeamData' element, and extract the pairs of values 'd_i' and 'v_i'.
Please follow the below code sample to proceed further,
xmlFile = 'yourfile.xml';
xDoc = xmlread(xmlFile);
% Navigate to the BeamData node or node of intrest.
beamDataNode = xDoc.getElementsByTagName('BeamData').item(0);
% Extract the text content of the BeamData node
dataStr = char(beamDataNode.getTextContent());
% Split the string by ';' to separate each pair
dataPairs = strsplit(dataStr, ';');
d_values = [];
v_values = [];
for i = 1:length(dataPairs)
if ~isempty(dataPairs{i})
% Split each pair by ','
values = strsplit(dataPairs{i}, ',');
% Convert strings to numbers and store in vectors
d_values(end+1) = str2double(values{1});
v_values(end+1) = str2double(values{2});
end
end
For a comprehensive understanding of the 'xmlread' and 'strsplit' functions in MATLAB, please refer to the following documentation.
I hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!