How to import an XML file that MATLAB?

조회 수: 3 (최근 30일)
setareh setareh
setareh setareh 2015년 10월 14일
답변: Jan 2015년 10월 14일
How to import an XML file that MATLAB? I am following code shows the following error:
function theStruct = parseXML(TS3)
% PARSEXML Convert XML file to a MATLAB structure.
try
tree = xmlread(TS3);
catch
error('Failed to read XML file %s.',TS3);
end
% Recurse over child nodes. This could run into problems
% with very deeply nested trees.
try
theStruct = parseChildNodes(tree);
catch
error('Unable to parse XML file %s.',TS3);
end
% ----- Local function PARSECHILDNODES -----
function children = parseChildNodes(theNode)
% Recurse over node children.
children = [];
if theNode.hasChildNodes
childNodes = theNode.getChildNodes;
numChildNodes = childNodes.getLength;
allocCell = cell(1, numChildNodes);
children = struct( ...
'Name', allocCell, 'Attributes', allocCell, ...
'Data', allocCell, 'Children', allocCell);
for count = 1:numChildNodes
theChild = childNodes.item(count-1);
children(count) = makeStructFromNode(theChild);
end
end
% ----- Local function MAKESTRUCTFROMNODE -----
function nodeStruct = makeStructFromNode(theNode)
% Create structure of node info.
nodeStruct = struct( ...
'Name', char(theNode.getNodeName), ...
'Attributes', parseAttributes(theNode), ...
'Data', '', ...
'Children', parseChildNodes(theNode));
if any(strcmp(methods(theNode), 'getData'))
nodeStruct.Data = char(theNode.getData);
else
nodeStruct.Data = '';
end
% ----- Local function PARSEATTRIBUTES -----
function attributes = parseAttributes(theNode)
% Create attributes structure.
attributes = []; if theNode.hasAttributes theAttributes = theNode.getAttributes; numAttributes = theAttributes.getLength; allocCell = cell(1, numAttributes); attributes = struct('Name', allocCell, 'Value', ... allocCell);
for count = 1:numAttributes
attrib = theAttributes.item(count-1);
attributes(count).Name = char(attrib.getName);
attributes(count).Value = char(attrib.getValue);
end
end
and I type in command window :
TS3='C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\matlab\general\TS3.xml' array(5000)=parseXML(TS3);
And the following error appears:
Error using parseXML (line 6)
Failed to read XML file C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\matlab\general\TS3.xml.

답변 (1개)

Jan
Jan 2015년 10월 14일
The error appears in the line:
tree = xmlread(TS3)
Unfortunately this line is included in a try-catch block without catching the error message. Better:
try
tree = xmlread(TS3);
catch ME
error('setareh:parseXML:invalidFile', ...
'Failed to read XML file %s.\n%s', TS3, ME.message);
end
Or use the debugger:
dbstop if all error
Then Matlab stops, when the error appears and you can inspect the local code lines.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by