Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Groovy解析xml大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

方式1:

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>
	<updatetiR_997_11845@e><![CDATA[2012-05-23 19:23:52]]></updatetiR_997_11845@e>
	<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所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。