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>
	<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)