matlab 读取 改写 .xml

参考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




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值