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

12 次查看(过去 30 天)
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!

采纳的回答

per isakson
per isakson 2015-4-15
编辑:per isakson 2015-4-16
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 个评论
Haley Inniger
Haley Inniger 2015-4-15
Thank you! This helps immensely. I had never heard of the regexp command before haha
Thanks again!
Ken Atwell
Ken Atwell 2015-4-17
Haley, regular expressions will change your life. :)

请先登录,再进行评论。

更多回答(1 个)

Patrick Lloyd
Patrick Lloyd 2015-4-15
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 个评论
Haley Inniger
Haley Inniger 2015-4-15
编辑:Haley Inniger 2015-4-15
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" >.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by