matlab读取xml文件的详细代码整理与实例分析

matlab读取xml文件的详细代码整理与实例分析

  1. 本文原始代码来源于

https://www.cnblogs.com/zhuangwy-cv/p/3729855.html

  1. 本文参考的原博客代码不完整有部分代码未上传,不能有效运行。因此本文在此基础进行了修改注释,形成一个可以正常运行且增强可读性的过程。

  2. 本文特点

    • 提供完整可以运行的代码
    • 详细的代码过程注释
    • 可用的实例数据
    • 骚话少
  3. 适用场景

    • 在matlab中对xml数据提取
    • 爬虫结果的分析
    • 基础入门教程
  4. 更新内容

    • 改为批量处理过程

原始代码

主函数

%% 读取特点结构xml文件中的内容,并保存在本地;
%% 2020年11月17日23:47:16
%% 尚帝@163.com
lear;
file_name='test-107.xml';%文件名
p = mfilename('fullpath'); %读取当前运行所在位置
[pathstr, name, ext] = fileparts(p);
filename=[pathstr,'\',file_name];
filename1=[pathstr,'\Results\']; %结果存储位置
xmlDoc = xmlread(filename);   % 读取文件  bodyannotation.xml

%% Extract ID
IDArray = xmlDoc.getElementsByTagName('filename');    % 将所有文件名节点放入数组IDArray
for i = 1 : IDArray.getLength    % 此例子中, IDArray.getLength 
    kth(i).pathnm= char(IDArray.item(i-1).getFirstChild.getData);    % 提取当前节点的内容
end
%% extract the FDs
FDsArray1 = xmlDoc.getElementsByTagName('object');  %   将所有object节点放入数组FDsArray
FDsArray = xmlDoc.getElementsByTagName('bndbox');  %   将所有bndbox节点放入数组FDsArray
a=zeros(FDsArray1.getLength,4); %预先数据分配存储的内存
number_box1=FDsArray1.getLength;%节点数 
 for i = 1 : FDsArray.getLength   
    thisItem = FDsArray.item(i-1);  %    
    childNode = thisItem.getFirstChild ;
    
  while ~isempty(childNode)  % 遍历bndbox的所有子节点,也就是遍历 标注程序保存下来的各个数据点 节点

      if  childNode.getNodeType == childNode.ELEMENT_NODE     % 检查当前节点没有子节点,  childNode.ELEMENT_NODE 定义为没有子节点。
          childNodeNm = char(childNode.getTagName);        % 当前节点的名字
          childNodedata = char(childNode.getFirstChild.getData);    % 当前节点的内容
          marks=choosenode(childNodeNm); %与要匹配的数据信息进行编号
          switch(marks)                                          
             case 1     
                 A.name=childNodedata;
             case 2     
                 A.pose=childNodedata;
             case 3  
                 A.bndbox=childNodedata;
                
             case 4
                 A.xmin=childNodedata;
             case 5
                 A.ymin=childNodedata;
             case 6
                 A.xmax=childNodedata;
             case 7
                 A.ymax=childNodedata;
             case 8
                 A.truncated=childNodedata;
             case 9
                 A.difficult=childNodedata;
              otherwise
          end
      end      
     childNode = childNode.getNextSibling;     % 切换到下一个节点
  end  % End WHILE
%% 写入数据到矩阵中
     a(i,1)=str2double(A.xmin);
     a(i,2)=str2double(A.ymin);
     a(i,3)=str2double(A.xmax);
     a(i,4)=str2double(A.ymax);
 end
s.box=a;
s.name=kth.pathnm;
s.number_box1=number_box1;
disp("提取的结果如下")
disp(s)
disp("box提取的结果如下")
disp(a)
ax=strcat(filename1,file_name,'.mat');
save  ax s %保存提取的结果

要设定要提取的标签并进行输出位置控制

function mark=choosenode(childNodeNm)

if  strcmp(childNodeNm,'name')
    mark=1;
end
if  strcmp(childNodeNm,'pose')
    mark=2;
end
if  strcmp(childNodeNm,'bndbox')
    mark=3;
end
if  strcmp(childNodeNm,'xmin')
    mark=4;
end
if  strcmp(childNodeNm,'ymin')
    mark=5;
end
if  strcmp(childNodeNm,'xmax')
    mark=6;
end
if  strcmp(childNodeNm,'ymax')
    mark=7;
end
if  strcmp(childNodeNm,'truncated')
    mark=8;
end
if  strcmp(childNodeNm,'difficult')
    mark=9;
end
if  strcmp(childNodeNm,'truncated')
    mark=8;
end

读取xml的展示

<annotation verified="no">
	<folder>positives</folder>
	<filename>test-107.pgm</filename>
	<path>E:/姘存灉鏁版嵁闆�/apple/test_images/positives/test-107.pgm</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>612</width>
		<height>459</height>
		<depth>1</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>apple</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>80</xmin>
			<ymin>190</ymin>
			<xmax>218</xmax>
			<ymax>338</ymax>
		</bndbox>
	</object>
	<object>
		<name>apple</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>178</xmin>
			<ymin>190</ymin>
			<xmax>357</xmax>
			<ymax>363</ymax>
		</bndbox>
	</object>
	<object>
		<name>apple</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>193</xmin>
			<ymin>319</ymin>
			<xmax>354</xmax>
			<ymax>413</ymax>
		</bndbox>
	</object>
	<object>
		<name>apple</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>339</xmin>
			<ymin>163</ymin>
			<xmax>562</xmax>
			<ymax>385</ymax>
		</bndbox>
	</object>
</annotation>

示例说明

本文为提取出bndbox中数据结构下的4个参数,并将参数以人能看懂的方式存储起来。

  1. 首先是利用xmlread将文件读取到matlab中
  2. 然后使用读取结果的getElementsByTagName属性设定要在xml中读取的数据,本文主要是读取bndbox结构下的数,因此将设定参数为FDsArray = xmlDoc.getElementsByTagName(‘bndbox’);
  3. 之后将遍历所有bndbox的子点,读取数据。

批量分析,更新代码

批量读取数据

p = mfilename('fullpath'); %读取当前运行所在位置
[pathstr, name, ext] = fileparts(p);
p = mfilename('fullpath'); %读取当前运行所在位置
% p_name=pathstr
filename=[pathstr,'\Results\'];%数据读取与存储位置
files = dir(strcat(filename,'*.xml')); 
[pathstr, name, ext] = fileparts(p);

for i=1:length(files)
  file_name=files(i).name;
 filename1=[pathstr,'\Results\',file_name]; %结果存储位置
 xlm_read_hung(file_name,filename1)
end

运行函数

function xlm_read_hung(file_name,filename1)
%% 读取特点结构xml文件中的内容,并保存在本地;、
%% 改成批量读取数据
%% 2020年11月18日15:41:47

% file_name='test-107.xml';%文件名
xmlDoc = xmlread(filename1);   % 读取文件  bodyannotation.xml

%% Extract ID
IDArray = xmlDoc.getElementsByTagName('filename');    % 将所有文件名节点放入数组IDArray
for i = 1 : IDArray.getLength    % 此例子中, IDArray.getLength 
    kth(i).pathnm= char(IDArray.item(i-1).getFirstChild.getData);    % 提取当前节点的内容
end
%% extract the FDs
FDsArray1 = xmlDoc.getElementsByTagName('object');  %   将所有object节点放入数组FDsArray
FDsArray = xmlDoc.getElementsByTagName('bndbox');  %   将所有bndbox节点放入数组FDsArray
a=zeros(FDsArray1.getLength,4); %预先数据分配存储的内存
number_box1=FDsArray1.getLength;%节点数 
 for i = 1 : FDsArray.getLength   
    thisItem = FDsArray.item(i-1);  %    
    childNode = thisItem.getFirstChild ;
    
  while ~isempty(childNode)  % 遍历bndbox的所有子节点,也就是遍历 标注程序保存下来的各个数据点 节点

      if  childNode.getNodeType == childNode.ELEMENT_NODE     % 检查当前节点没有子节点,  childNode.ELEMENT_NODE 定义为没有子节点。
          childNodeNm = char(childNode.getTagName);        % 当前节点的名字
          childNodedata = char(childNode.getFirstChild.getData);    % 当前节点的内容
          marks=choosenode(childNodeNm); %与要匹配的数据信息进行编号
          switch(marks)                                          
             case 1     
                 A.name=childNodedata;
             case 2     
                 A.pose=childNodedata;
             case 3  
                 A.bndbox=childNodedata;
                
             case 4
                 A.xmin=childNodedata;
             case 5
                 A.ymin=childNodedata;
             case 6
                 A.xmax=childNodedata;
             case 7
                 A.ymax=childNodedata;
             case 8
                 A.truncated=childNodedata;
             case 9
                 A.difficult=childNodedata;
              otherwise
          end
      end      
     childNode = childNode.getNextSibling;     % 切换到下一个节点
  end  % End WHILE
%% 写入数据到矩阵中
     a(i,1)=str2double(A.xmin);
     a(i,2)=str2double(A.ymin);
     a(i,3)=str2double(A.xmax);
     a(i,4)=str2double(A.ymax);
 end
s.box=a;
s.name=kth.pathnm;
s.number_box1=number_box1;
disp("提取的结果如下")
disp(s)
disp("box提取的结果如下")
disp(a)
ax=strcat(file_name,'.mat');
save(strcat(filename1(1:end-4),'.pgm.mat'),'s') % 存储的地址和名称

  • 10
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值