Groovy解析xml

Groovy解析xml,解析过程超简洁

方式1:

Java代码   收藏代码
  1. static main(args) {  
  2.         File xmls =new File("");  
  3.         def asset = new XmlParser().parse("c:/CCTV1_20120523_185942738_770297_117912.xml");  
  4.         def title=asset.title[0].text()  
  5.         def keywords=asset.keywords[0].text()  
  6.         def coloumnName=asset.coloumnName[0].text()  
  7.         def categoryName=asset.categoryName[0].text()  
  8.         def duration=asset.duration[0].text()  
  9.         def imagePath=asset.imagePath[0].text()  
  10.         println title  
  11.         println keywords  
  12.         println coloumnName  
  13.         println categoryName  
  14.         println duration  
  15.         println imagePath  
  16.     }  

 方式2:

 

Java代码   收藏代码
  1. static main(args){  
  2.         def books = new XmlParser().parse("c:/b.xml");  
  3.         def bookss = books.book;  
  4.         def specialBook = bookss[3];  
  5.         books.book.each {  
  6.             def bookid = it.attributes().get("bookid");  
  7.             println bookid;  
  8.             def properties = specialBook.position[0].property;   
  9.             properties.each {  
  10.                    println "-------------------"  
  11.                    println it.'@name';  
  12.                    println it.text();  
  13.                    println "-------------------"  
  14.                 }  
  15.         }  
  16.     }  

 两个xml文件如下:

 

CCTV1_20120523_185942738_770297_117912.xml

 

Xml代码   收藏代码
  1. <?xml version='1.0' encoding='utf-8' ?>  
  2. <Asset>  
  3.     <uuid>882657bc-698f-4a71-9d3a-3c95f4138c63</uuid>  
  4.     <id></id>  
  5.     <playTime><![CDATA[2012-05-23 19:01:40]]></playTime>  
  6.     <author>TVM</author>  
  7.     <language>Chinese</language>  
  8.     <title><![CDATA[纪念同志《在延安文艺座谈会上的讲话》发表70周年座谈会在京召开]]></title>  
  9.     <content><![CDATA[]]></content>  
  10.     <keywords><![CDATA[纪念部]]></keywords>  
  11.     <channelCName>CCTV-1</channelCName>  
  12.     <channelName>CCTV1</channelName>  
  13.     <coloumnName>新闻联播</coloumnName>  
  14.     <imagePath><![CDATA[News/JPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></imagePath>  
  15.     <categoryName>时政</categoryName>  
  16.     <categoryId>25</categoryId>  
  17.     <columnId>16</columnId>  
  18.     <channelId>16</channelId>  
  19.     <duration>290232</duration>  
  20.     <updateTime><![CDATA[2012-05-23 19:23:52]]></updateTime>  
  21.     <AssetFiles>  
  22.         <fileCount>9</fileCount>  
  23.         <file>  
  24.             <fileFormat>AVI</fileFormat>  
  25.             <filePath><![CDATA[News/AVI/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.avi]]></filePath>  
  26.             <videoCodec>Xvid</videoCodec>  
  27.             <videoFrameRate>25</videoFrameRate>  
  28.             <videoBitRate>1500</videoBitRate>  
  29.             <width>704</width>  
  30.             <hight>576</hight>  
  31.             <audioCodec>MEPG</audioCodec>  
  32.             <audioBitRate>128</audioBitRate>  
  33.             <audioFrequency>44100</audioFrequency>  
  34.             <duration>290232</duration>  
  35.             <fileSize></fileSize>  
  36.         </file>  
  37.         <file>  
  38.             <fileFormat>FLV</fileFormat>  
  39.             <filePath><![CDATA[News/FLV/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.flv]]></filePath>  
  40.             <videoCodec>H.263</videoCodec>  
  41.             <videoFrameRate>25</videoFrameRate>  
  42.             <videoBitRate>100</videoBitRate>  
  43.             <width>320</width>  
  44.             <hight>240</hight>  
  45.             <audioCodec>MEPG</audioCodec>  
  46.             <audioBitRate>64</audioBitRate>  
  47.             <audioFrequency>44100</audioFrequency>  
  48.             <duration>290232</duration>  
  49.             <fileSize></fileSize>  
  50.         </file>  
  51.         <file>  
  52.             <fileFormat>NFLV</fileFormat>  
  53.             <filePath><![CDATA[News/NFLV/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.flv]]></filePath>  
  54.             <videoCodec>H.264</videoCodec>  
  55.             <videoFrameRate>25</videoFrameRate>  
  56.             <videoBitRate>400</videoBitRate>  
  57.             <width>480</width>  
  58.             <hight>360</hight>  
  59.             <audioCodec>MEPG</audioCodec>  
  60.             <audioBitRate>32</audioBitRate>  
  61.             <audioFrequency>44100</audioFrequency>  
  62.             <duration>290232</duration>  
  63.             <fileSize></fileSize>  
  64.         </file>  
  65.         <file>  
  66.             <fileFormat>MP4_MAIN</fileFormat>  
  67.             <filePath><![CDATA[News/MP4_MAIN/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mp4]]></filePath>  
  68.             <videoCodec>AVC</videoCodec>  
  69.             <videoFrameRate>25</videoFrameRate>  
  70.             <videoBitRate>512</videoBitRate>  
  71.             <width>400</width>  
  72.             <hight>300</hight>  
  73.             <audioCodec>AAC</audioCodec>  
  74.             <audioBitRate>48</audioBitRate>  
  75.             <audioFrequency>48000</audioFrequency>  
  76.             <duration>290232</duration>  
  77.             <fileSize></fileSize>  
  78.         </file>  
  79.         <file>  
  80.             <fileFormat>MP4</fileFormat>  
  81.             <filePath><![CDATA[News/MP4/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mp4]]></filePath>  
  82.             <videoCodec>AVC</videoCodec>  
  83.             <videoFrameRate>25</videoFrameRate>  
  84.             <videoBitRate>250</videoBitRate>  
  85.             <width>352</width>  
  86.             <hight>288</hight>  
  87.             <audioCodec>AAC</audioCodec>  
  88.             <audioBitRate>32</audioBitRate>  
  89.             <audioFrequency>44100</audioFrequency>  
  90.             <duration>290232</duration>  
  91.             <fileSize></fileSize>  
  92.         </file>  
  93.         <file>  
  94.             <fileFormat>JPG</fileFormat>  
  95.             <filePath><![CDATA[News/JPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></filePath>  
  96.             <width>704</width>  
  97.             <hight>576</hight>  
  98.             <fileSize></fileSize>  
  99.         </file>  
  100.         <file>  
  101.             <fileFormat>SJPG</fileFormat>  
  102.             <filePath><![CDATA[News/SJPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></filePath>  
  103.             <width>96</width>  
  104.             <hight>72</hight>  
  105.             <fileSize></fileSize>  
  106.         </file>  
  107.         <file>  
  108.             <fileFormat>MTG</fileFormat>  
  109.             <filePath><![CDATA[News/MTG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mtg]]></filePath>  
  110.             <fileSize></fileSize>  
  111.         </file>  
  112.         <file>  
  113.             <fileFormat>TMF</fileFormat>  
  114.             <filePath><![CDATA[]]></filePath>  
  115.             <fileSize></fileSize>  
  116.         </file>  
  117.     </AssetFiles>  
  118. </Asset>  

  b.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <books location="theLocation">  
  3.   <book bookid="1" mark="4">Java in action</book>  
  4.   <book bookid="2" mark="3">Groovy in action</book>  
  5.   <book bookid="3" mark="2">JavaScript in action</book>  
  6.   <book bookid="4">  
  7.       <position url="http://">  
  8.          <property name="propertiName1">Hello</property>  
  9.          <property name="propertiName2">World</property>  
  10.       </position>  
  11.   </book>  
  12. </books>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值