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

5 visualizaciones (últimos 30 días)
Ahmed
Ahmed el 6 de Mzo. de 2016
Editada: Walter Roberson el 6 de Mzo. de 2016
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 comentarios
Image Analyst
Image Analyst el 6 de Mzo. de 2016
Can you first explain why you're not using xmlread()?
Ahmed
Ahmed el 6 de Mzo. de 2016
Editada: Ahmed el 6 de Mzo. de 2016
I used it and I got out of memory my file size 1.5GB @Image Analyst

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 6 de Mzo. de 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 comentarios
Image Analyst
Image Analyst el 6 de Mzo. de 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
Ahmed
Ahmed el 6 de Mzo. de 2016
Editada: Ahmed el 6 de Mzo. de 2016
Thanks but I have a different values each time in nodes and edges. each one has a unique Id and label

Iniciar sesión para comentar.

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by