花開有時

          花開有時,花落有時,來有時,去有時。不撕扯,只關(guān)切;不紀念,只牽掛;不感動,只明白;不尋找,只記得。
          隨筆 - 24, 文章 - 0, 評論 - 54, 引用 - 0
          數(shù)據(jù)加載中……

          用Groovy讀XML文件。

          Groovy提供了更簡單的方法進行XML文件的讀取。

          下面是要讀取的XML文件pla.xml

          <plan>

          <week capacity="8">

          <task done="2" total="2" title="read XML chapter"/>

          <task done="3" total="3" title="try some reporting"/>

          <task done="1" total="2" title="use in current project"/>

          </week>

          <week capacity="8">

          <task done="0" total="1" title="re-read DB chapter"/>

          <task done="0" total="3" title="use DB/XML combination"/>

          </week>

          </plan>

          下面是代碼:

          def node = new XmlParser().parse(new File('data/plan.xml'))

          def path = new XmlSlurper().parse(new File('data/plan.xml'))

          assert 'plan' == node.name()

          assert 'plan' == path.name()

          assert 2 == node.children().size()

          assert 2 == path.children().size()

          assert 5 == node.week.task.size()

          assert 5 == path.week.task.size()

          assert 6 == node.week.task.'@done'*.toInteger().sum()

          assert path.week[1].task.every{ it.'@done' == '0' }

          Groovy提供了兩個類進行XML文件的讀取:XmlParser類和XmlSlurper類。這兩個類的功能基本差不多,但是讀的方法不同。概要的說,XmlParser類需要的內(nèi)存更大些,它需要把整個XML文件先讀取到內(nèi)存中,在按要求進行檢索,適合小文件。XmlSlurper則是需要什么內(nèi)容就讀什么內(nèi)容,可能速度慢些。具體區(qū)別與用法可參看《Groovy in Action》的443頁。

          posted on 2007-09-25 10:12 花開有時 閱讀(1973) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 克什克腾旗| 潢川县| 军事| 上饶市| 思茅市| 甘德县| 屏山县| 谷城县| 千阳县| 绥化市| 天等县| 榆林市| 辽宁省| 团风县| 普安县| 福泉市| 昌都县| 齐齐哈尔市| 吴堡县| 谢通门县| 句容市| 临江市| 德令哈市| 九龙城区| 荔波县| 济南市| 馆陶县| 星座| 喜德县| 长葛市| 界首市| 太原市| 文登市| 双辽市| 永州市| 霍城县| 阳谷县| 雅安市| 贵德县| 鄂尔多斯市| 麟游县|