Read Xml file line by line in Matlab and save node attribute
7 views (last 30 days)
Show older comments
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);
2 Comments
Answers (1)
Image Analyst
on 6 Mar 2016
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 Comments
Image Analyst
on 6 Mar 2016
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
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!