Read Xml file line by line in Matlab and save node attribute

9 次查看(过去 30 天)
I have a very large Xml file and I want to read it line by line in Matlab. my question is how can I get node attribute between these tags:
<node id="38942" label="Q8NBU5"> <node>
<edge id="9167" label="P05067 (EBI-8038603) P78352" source="2604" target="4629" cy:directed="1"> <edge>
I want to get id and label values from node and id , label,source and target form edges Tried this code but I did not get anything just the nodes number.
Any help will be highly appreciate
Here is my attempt
clc
clear all
n=0;
fid = fopen('Int.xml','rt'); % 'rt' means "read text"
while 1
line = fgetl(fid); if ~ischar(line), break, end
if ~isempty(strfind(line,'<node')),
n = n + 1;
D(n)= nodes.item(n).getAttribute(line,'id');
end
end
fclose(fid);

回答(1 个)

Image Analyst
Image Analyst 2016-3-6
Search for <node id="38942" with strfind(). Then inside the if, call fgetl() again to get the line of code inside the tag. Then extract the part of the line you want with indexing, like
myAttribute = thisLine(3:15); % or whatever.
Set a flag or flags so that when you've located each you can "break".
DO NOT USE line as the name of your variable since it's the name of a built-in function. Call it thisLine instead.
  3 个评论
Image Analyst
Image Analyst 2016-3-6
Something like
found38942 = false;
found9167 = false;
while ~found38942 && ~found9167
thisLine = fgetl(fid); if ~ischar(line), break, end
if ~isempty(strfind(thisLine,'<node id="38942"')),
n = n + 1;
% Get the next line
thisLine = fgetl(fid);
D(n)= thisLine(3:15); % Whatever
found38942 = true;
end
if ~isempty(strfind(thisLine,'<edge id="9167"')),
n = n + 1;
% Get the next line
thisLine = fgetl(fid);
D(n)= thisLine(3:15); % Whatever
found9167= true;
end
end
Ahmed
Ahmed 2016-3-6
编辑:Ahmed 2016-3-6
Thanks but I have a different values each time in nodes and edges. each one has a unique Id and label

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by