Groovy解析xml,解析过程超简洁
方式1:
@H_502_10@static main(args) { file xmls =new file(""); def asset = new XmlParser().parse("c:/CCTV1_20120523_185942738_770297_117912.xml"); def Title=asset.Title[0].text() def keywords=asset.keywords[0].text() def coloumnname=asset.coloumnname[0].text() def categoryname=asset.categoryname[0].text() def duration=asset.duration[0].text() def imagePath=asset.imagePath[0].text() println Title println keywords println coloumnname println categoryname println duration println imagePath }方式2:
def xmlfile = new XmlParser().parse("c:/b.xml"); //查找排序例子 xmlfile.book.findAll { it.attributes().keySet().contains("mark"); }.sort{ it.'@mark'.toInteger(); //排序的依据 }.each { println it.'@bookID'; } def books = xmlfile.book; def specialBook = books[3]; assert specialBook.'@bookID' == "4"; def propertIEs = specialBook.position[0].property; //获取position的值; propertIEs.each { println "-------------------" println it.'@name'; println it.text(); println "-------------------" }
两个xml文件如下:
CCTV1_20120523_185942738_770297_117912.xml
<?xml version='1.0' enCoding='utf-8' ?><Asset> <uuID>882657bc-698f-4a71-9d3a-3c95f4138c63</uuID> <ID></ID> <playTime><![cdaTA[2012-05-23 19:01:40]]></playTime> <author>TVM</author> <language>Chinese</language> <Title><![cdaTA[纪念同志《在延安文艺座谈会上的讲话》发表70周年座谈会在京召开]]></Title> <content><![cdaTA[]]></content> <keywords><![cdaTA[纪念部]]></keywords> <channelCname>CCTV-1</channelCname> <channelname>CCTV1</channelname> <coloumnname>新闻联播</coloumnname> <imagePath><![cdaTA[News/JPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></imagePath> <categoryname>时政</categoryname> <categoryID>25</categoryID> <columnID>16</columnID> <channelID>16</channelID> <duration>290232</duration> <updateTime><![cdaTA[2012-05-23 19:23:52]]></updateTime> <Assetfiles> <fileCount>9</fileCount> <file> <fileFormat>AVI</fileFormat> <filePath><![cdaTA[News/AVI/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.avi]]></filePath> <vIDeoCodec>XvID</vIDeoCodec> <vIDeoFrameRate>25</vIDeoFrameRate> <vIDeoBitRate>1500</vIDeoBitRate> <wIDth>704</wIDth> <hight>576</hight> <audioCodec>MEPG</audioCodec> <audioBitRate>128</audioBitRate> <audioFrequency>44100</audioFrequency> <duration>290232</duration> <fileSize></fileSize> </file> <file> <fileFormat>FLV</fileFormat> <filePath><![cdaTA[News/FLV/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.flv]]></filePath> <vIDeoCodec>H.263</vIDeoCodec> <vIDeoFrameRate>25</vIDeoFrameRate> <vIDeoBitRate>100</vIDeoBitRate> <wIDth>320</wIDth> <hight>240</hight> <audioCodec>MEPG</audioCodec> <audioBitRate>64</audioBitRate> <audioFrequency>44100</audioFrequency> <duration>290232</duration> <fileSize></fileSize> </file> <file> <fileFormat>NFLV</fileFormat> <filePath><![cdaTA[News/NFLV/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.flv]]></filePath> <vIDeoCodec>H.264</vIDeoCodec> <vIDeoFrameRate>25</vIDeoFrameRate> <vIDeoBitRate>400</vIDeoBitRate> <wIDth>480</wIDth> <hight>360</hight> <audioCodec>MEPG</audioCodec> <audioBitRate>32</audioBitRate> <audioFrequency>44100</audioFrequency> <duration>290232</duration> <fileSize></fileSize> </file> <file> <fileFormat>MP4_MAIN</fileFormat> <filePath><![cdaTA[News/MP4_MAIN/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mp4]]></filePath> <vIDeoCodec>AVC</vIDeoCodec> <vIDeoFrameRate>25</vIDeoFrameRate> <vIDeoBitRate>512</vIDeoBitRate> <wIDth>400</wIDth> <hight>300</hight> <audioCodec>AAC</audioCodec> <audioBitRate>48</audioBitRate> <audioFrequency>48000</audioFrequency> <duration>290232</duration> <fileSize></fileSize> </file> <file> <fileFormat>MP4</fileFormat> <filePath><![cdaTA[News/MP4/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mp4]]></filePath> <vIDeoCodec>AVC</vIDeoCodec> <vIDeoFrameRate>25</vIDeoFrameRate> <vIDeoBitRate>250</vIDeoBitRate> <wIDth>352</wIDth> <hight>288</hight> <audioCodec>AAC</audioCodec> <audioBitRate>32</audioBitRate> <audioFrequency>44100</audioFrequency> <duration>290232</duration> <fileSize></fileSize> </file> <file> <fileFormat>JPG</fileFormat> <filePath><![cdaTA[News/JPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></filePath> <wIDth>704</wIDth> <hight>576</hight> <fileSize></fileSize> </file> <file> <fileFormat>SJPG</fileFormat> <filePath><![cdaTA[News/SJPG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.jpg]]></filePath> <wIDth>96</wIDth> <hight>72</hight> <fileSize></fileSize> </file> <file> <fileFormat>MTG</fileFormat> <filePath><![cdaTA[News/MTG/CCTV1/2012/05/23/CCTV1_20120523_185942738_770297_117912.mtg]]></filePath> <fileSize></fileSize> </file> <file> <fileFormat>TMF</fileFormat> <filePath><![cdaTA[]]></filePath> <fileSize></fileSize> </file> </Assetfiles></Asset>
b.xml
<?xml version="1.0" enCoding="UTF-8"?><books location="theLocation"> <book bookID="1" mark="4">Java in action</book> <book bookID="2" mark="3">Groovy in action</book> <book bookID="3" mark="2">JavaScript in action</book> <book bookID="4"> <position url="http://"> <property name="propertiname1">Hello</property> <property name="propertiname2">World</property> </position> </book></books>总结
以上是内存溢出为你收集整理的Groovy解析xml全部内容,希望文章能够帮你解决Groovy解析xml所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)