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

조회 수: 5 (최근 30일)
Ahmed
Ahmed 2016년 3월 6일
편집: Walter Roberson 2016년 3월 6일
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
Image Analyst
Image Analyst 2016년 3월 6일
Can you first explain why you're not using xmlread()?
Ahmed
Ahmed 2016년 3월 6일
편집: Ahmed 2016년 3월 6일
I used it and I got out of memory my file size 1.5GB @Image Analyst

댓글을 달려면 로그인하십시오.

답변 (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에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by