필터 지우기
필터 지우기

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

조회 수: 17 (최근 30일)
Haley Inniger
Haley Inniger 2015년 4월 15일
댓글: Ken Atwell 2015년 4월 17일
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