5、Groovy創建XML與解析XML實現

1、book.xml文件結構

<?xml version="1.0" encoding="UTF-8" ?>
<Books>
    <Book id="1">
        <title>Kotlin入門到實戰</title>
        <author>周伯通</author>
    </Book>
    <Book id="2">
        <title>Groovy入門到實戰</title>
        <author>楊過</author>
    </Book>
    <Book id="3">
        <title>Scala入門到實戰</title>
        <author>小龍女</author>
    </Book>
    <Book id="4">
        <title>Java入門到實戰</title>
        <author>郭靖</author>
    </Book>
    <Book id="5">
        <title>Swift入門到精通</title>
        <author>洪七公</author>
    </Book>
</Books>

2、代碼實現

  • 此代碼可以創建一個XmlTest類即可,Books與Book都是在XmlTest類,此類爲了方便閱讀,不創建多個類進行實現,由於代碼較少。這裏提供了2種創建xml的實現,與一種解析xml的實現
package com.groovy.domain

import groovy.xml.MarkupBuilder
import groovy.xml.XmlSlurper
import org.junit.Test

//Books對象
class Books {
    int count = 5
    def books = [
            new Book(id: System.currentTimeMillis(),title: "Kotlin入門到實戰", author: "周伯通"),
            new Book(id: System.currentTimeMillis(),title: "Swift入門到實戰", author: "楊過"),
            new Book(id: System.currentTimeMillis(),title: "Groovy入門到實戰", author: "小龍女"),
            new Book(id: System.currentTimeMillis(),title: "Scala入門到實戰", author: "郭靖"),
            new Book(id: System.currentTimeMillis(),title: "Java入門到實戰", author: "瑛姑")
    ]
}

//Book對象
class Book {
    Long id
    String title
    String author
}

class XmlTest {

    @Test
    void createXmlObject() {

        def sw = new StringWriter()
        def markupBuilder = new MarkupBuilder(sw)
        def books = new Books()

        //創建一個Books節點
        markupBuilder.Books(count: books.count) {
            //獲取books數據進行遍歷
            books.books.forEach { book ->
                //獲取book數據,創建一個Book節點
                Book(id:book.id,title: book.title, author: book.author)
            }
        }
        println(sw)
    }

    @Test
    void createXml() {

        def sw = new StringWriter()
        def markupBuilder = new MarkupBuilder(sw)
        //對Books節點添加一個count屬性值爲5
        markupBuilder.Books(count: 5) {
            //在Books創建Book節點,並且給一個id爲1的值
            //在book節點下創建title與author節點
            Book(id: 1) {
                title("Kotlin入門到實戰")
                author("周伯通")
            }
            Book(id: 2) {
                title("Java入門到實戰")
                author("楊過")
            }
            Book(id: 3) {
                title("Groovy入門到實戰")
                author("小龍女")
            }
            Book(id: 4) {
                title("Scala入門到實戰")
                author("瑛姑")
            }
            Book(id: 5) {
                title("Swift入門到實戰")
                author("郭靖")
            }
        }
        println(sw)
    }


    @Test
    void parseXml() {

        //獲取xml文件路徑
        def path = XmlTest.class.getClassLoader().getResource("book.xml").path

        //指定文件路徑,指定解析編碼進行獲內容信息
        def content = new File(path).getText("utf-8")

        //創建XmlSlurper對象
        def xmlSlurper = new XmlSlurper()
        //解析內容信息,獲取book節點
        def books = xmlSlurper.parseText(content)
        // println(books.Book[0].@id)
        //獲取多個Book節點
        books.Book.forEach { book ->
            //使用GPathResult節點路徑去獲取id值
            println("Id=${book.@id},書名稱=${book.title},書作者=${book.author}")
        }
    }

}

3、createXmlObject方法運行的結果

<Books count='5'>
  <Book id='1638084704535' title='Kotlin入門到實戰' author='周伯通' />
  <Book id='1638084704559' title='Swift入門到實戰' author='楊過' />
  <Book id='1638084704559' title='Groovy入門到實戰' author='小龍女' />
  <Book id='1638084704560' title='Scala入門到實戰' author='郭靖' />
  <Book id='1638084704560' title='Java入門到實戰' author='瑛姑' />
</Books>

4、createXml方法運行的結果

<Books count='5'>
  <Book id='1'>
    <title>Kotlin入門到實戰</title>
    <author>周伯通</author>
  </Book>
  <Book id='2'>
    <title>Java入門到實戰</title>
    <author>楊過</author>
  </Book>
  <Book id='3'>
    <title>Groovy入門到實戰</title>
    <author>小龍女</author>
  </Book>
  <Book id='4'>
    <title>Scala入門到實戰</title>
    <author>瑛姑</author>
  </Book>
  <Book id='5'>
    <title>Swift入門到實戰</title>
    <author>郭靖</author>
  </Book>
</Books>

5、parseXml方法運行的結果

Id=1,書名稱=Kotlin入門到實戰,書作者=周伯通
Id=2,書名稱=Groovy入門到實戰,書作者=楊過
Id=3,書名稱=Scala入門到實戰,書作者=小龍女
Id=4,書名稱=Java入門到實戰,書作者=郭靖
Id=5,書名稱=Swift入門到精通,書作者=洪七公
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章