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
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
 
Easier to read
str = fileread( 'cssm.txt' )
abq = '([^"]+)'; % anything but quotation mark
xpr = ['<Nextline name="',abq,'" value="',abq,'">'];
cac = regexp( str, xpr, 'tokens');
2 个评论
更多回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!