How do I access the complex text in an xml file?
Show older comments
I have multiple xml files that have this format:
<Element>
<Attribute value="2.0">
<Nextline name="Hello" value="9999">
<item name="data" value="111">
</Attribute>
</Element>
I want to access the name and value of Nextline and be able to write them into an excel document. If anyone has any advice on how to do this or what I could try, all advice is welcome. I've searched online and have yet to find anything helpful.
Also, if there is a good tutorial for using xml in MATLAB I would love to hear about it!
1 Comment
per isakson
on 15 Apr 2015
Accepted Answer
More Answers (1)
Patrick Lloyd
on 15 Apr 2015
I have some XML files that I parse like so:
function struct_out = my_xmlread(xml_in)
% Open file in read mode with fopen() and next line information
fid = fopen(xml_in,'r');
tline = fgetl(fid);
% Empty struct creation
struct_out = struct('varname', {}, 'datatype', {});
% count tracks of each parameter
count = 1;
% Loops line by line until end of file is reached. It would be more
% robust w.r.t. string variations (and more importantly cooler) to use
% regular expressions to search through this. In its current form, the
% tags are presumed to have fixed lengths and params are parsed using
% string indexing.
while ~feof(fid)
if strcmp(tline,'<Name>VARIABLE NAME</Name>')
tline = fgetl(fid);
struct_out(count).varname = tline(6:end-14);
elseif strcmp(tline,'<Name>TYPE</Name>')
tline = fgetl(fid);
struct_out(count).datatype = tline(6:end-6);
cout = count + 1;
end % if strcmp(tline,'<Name>...</Name>')
% Get the next line
tline = fgetl(fid);
end % while ~feof(fid)
% Close file after reading
fclose(fid);
end % struct_out = xmlread(xml_in)
It's probably not the best way of doing this but the XML files are all very similar so shortcuts like string indexing can be used. The XML I use looks something like:
<String>
<Name>VARIABLE NAME</Name>
<Val>I_AM_THE_PARAMETER (COM1)</Val>
</String>
<String>
<Name>TYPE</Name>
<Val>REAL</Val>
</String>
My application isn't identical to yours but some of the techniques may be useful for your application. There's also a built-in xmlread() function but I don't really know how to use that effectively.
1 Comment
Haley Inniger
on 15 Apr 2015
Edited: Haley Inniger
on 15 Apr 2015
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!