在matlab中为多个读取的xml文件创建DOM对象的数组 [英] create arrays of DOM object in matlab for multiple read xml files
问题描述
我有很多XML文件,并按如下方式获取其文件名:
I have many XML files, and get their filenames like so:
[FileName,PathName] = uigetfile('*.xml','MultiSelect','on');
我想使用以下代码读取这些文件:
I want to read these files using this code:
for i=1:length(fullfile(FileName))
xtree(i) = xmlread(char(fullfile(FileName(i))));
但是我得到一个错误.单个XML文件效果很好:
but I got an error. It works well for a single XML file:
xtree = xmlread(char(fullfile(FileName)));
推荐答案
您没有正确使用单元格数组.你知道他们是什么吗?在Matlab中键入help cell
可以了解更多信息.
You're not using cell arrays properly. Do you know what they are? Type help cell
in Matlab to find out more.
基本上,这里发生的是uigetfile
输出名为FileName
的文件名的单元格数组.您可以通过大括号索引({}
)访问每个文件名.
Basically, what's going on here is that uigetfile
outputs a cell-array of filenames called FileName
. You access each filename by curcly-brace indexing ({}
).
类似地,每个节点都是一个特殊的对象,普通的Marlab数组无法正确捕获.最好也将xtree
定义为单元数组,并再次使用花括号将xmlread
的输出分配.
Similarly, each node is a special object that cannot be captured properly by a normal Marlab array. You're better off defining xtree
as a cell-array as well, and assign the output of xmlread
again with curly braces.
也:在运行时读取用户输入的文件时,总是更好地广泛检查错误.
Also: when reading files input by the user at runtime, it is always better to extensively check for errors.
总结:
[FileName,PathName] = uigetfile('*.m','MultiSelect','on');
if isequal(FileName,0) || isequal(PathName,0)
error('Cancel pressed.');
else
xtree = cell(size(FileName));
for i=1:numel(FileName)
try
xtree{i} = xmlread(fullfile([PathName FileName{i}]));
catch ME
%# handle error
end
end
end
这篇关于在matlab中为多个读取的xml文件创建DOM对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!