How do I access the complex text in an xml file?

19 views (last 30 days)
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!

Accepted Answer

per isakson
per isakson on 15 Apr 2015
Edited: per isakson on 16 Apr 2015
A quick and dirty variant:
str = fileread( 'cssm.txt' )
cac = regexp( str, '(?<=<Nextline name=")([^"]+)" value="([^"]+)">', 'tokens')
cac =
{1x2 cell} {1x2 cell}
>> cac{2}
ans =
'Hello' '9999'
where cssm.txt contains two sets of your sample text
&nbsp
Easier to read
str = fileread( 'cssm.txt' )
abq = '([^"]+)'; % anything but quotation mark
xpr = ['<Nextline name="',abq,'" value="',abq,'">'];
cac = regexp( str, xpr, 'tokens');
  2 Comments
Haley Inniger
Haley Inniger on 15 Apr 2015
Thank you! This helps immensely. I had never heard of the regexp command before haha
Thanks again!
Ken Atwell
Ken Atwell on 17 Apr 2015
Haley, regular expressions will change your life. :)

Sign in to comment.

More Answers (1)

Patrick Lloyd
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
Haley Inniger on 15 Apr 2015
Edited: Haley Inniger on 15 Apr 2015
Thanks for your response! Part of my project is working with what you are working with above and I am using a similar technique as you to parse that. However I have found that working with the text nodes like < Name> Variable Name < /Name> is different that working with the complex text elements like < Name value="Hello" >.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!