参考http://www.cnblogs.com/emituofo/archive/2011/11/06/2238219.html
推荐大家使用xml_io_tools工具包,下载地址:http://www.mathworks.com/matlabcentral/fileexchange/12907-xmliotools
使用该包的示例如下 : 假设.xml文件的路径为 fileNames
<?xml version="1.0" encoding="utf-8"?>
<annotation>
<folder>ISCAS</folder>
<filename>000074_0001.jpg</filename>
<source>
<database>iscas</database>
<annotation>iscas</annotation>
<image>iscas</image>
</source>
<size>
<width>1480</width>
<height>1374</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<item>
<name>ship</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmax>1356</xmax>
<xmin>123</xmin>
<ymax>1250</ymax>
<ymin>812</ymin>
</bndbox>
</item>
<item>
<name>ship</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmax>1277</xmax>
<xmin>259</xmin>
<ymax>616</ymax>
<ymin>240</ymin>
</bndbox>
</item>
<item>
<name>ship</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmax>1297</xmax>
<xmin>266</xmin>
<ymax>564</ymax>
<ymin>172</ymin>
</bndbox>
</item>
<item>
<name>ship</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmax>1302</xmax>
<xmin>279</xmin>
<ymax>503</ymax>
<ymin>104</ymin>
</bndbox>
</item>
</object>
</annotation>
应用示例:
[ annotation, RootName, Domnode] = xml_read( fileNames ) ; %%%读入.xml数据
img = [] ;
img = imread( img_name ) ;
annotation.size.width = size( img, 2) ; % this width. %%%% 更改相应的属性值
annotation.size.height = size( img, 1) ; % this height
xml_write( [ rootPath_new, '/', fileNames ] , annotation) ; %%% 存储文件
----------------------------------------------------------------------------------------------------------------------------------------------------------------
clear
Video = xml_read('regular.xml');%读取XML
TitleContent=Video.Title;%获取Title节点的文本内容
Slide1Content=Video.Slide(1).CONTENT;%获取第一个Slide节点的文本内容
Slide1Time=Video.Slide(1).ATTRIBUTE.Time;%获取第一个Slide节点的Time属性
Video.Slide(1).CONTENT='Good morning!';%修改第一个Slide节点的文本内容
Video.Slide(1).ATTRIBUTE.Time='0.05';%修改第一个Slide节点的Time属性
Video.Slide(1).ATTRIBUTE.Date='2011/11/14';%为第一个Slide节点添加Date属性,并设置为'2011/11/14'
% 新建节点,插入节点
newSlide=struct;%新建newSlide节点
newSlide.CONTENT='That''s all!';%设置文本内容
newSlide.ATTRIBUTE.Time='2:00';%设置Time属性
newSlide.ATTRIBUTE.Headline='Thank you!';%设置Headline属性
Video.Slide(end+1)=newSlide;%将newSlide插入Video的末尾
wPref.StructItem = false;%设置:重复节点写入时,不使用'item'
xml_write('regular_output.xml',Video,'Video',wPref);%保存XML