如何使用groovy读取和解析xml文件

oxf4rvwz  于 2022-12-03  发布在  其他
关注(0)|答案(3)|浏览(158)

我可以知道如何用groovy读取和解析.xml文件吗?groovy文件需要读取xml并获取商店ID和国家/地区信息

<?xml version="1.0"?>
<Someinformation>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>
<Shops>
    <shop id ="shop1" index ="1">
    <ctr  country="Japan">01</ctr>
    <ctr  country="Korea">02</ctr>
    </shop>
    <shop id ="shop2" index ="2">
    <ctr  country="England">03</ctr>
    <ctr  country="Germany">04</ctr>
    </shop>
</Shops>
</Someinformation>

要打开.xml文件,请执行以下操作:

def xml=new XmlSlurper().parse("book.xml")

但是如何获取xml内容呢?

wmomyfyw

wmomyfyw1#

你可以这样做:

def xml = new  groovy.xml.XmlSlurper().parse("book.xml")

def total = xml.'*'.size()
println "Total amount of books: $total"
for (i in 0..<total) {
    def book = xml.book[i]
    println "-------------------------"
    println "ID: ${book.@id.text()}"
    println "Author: ${book.author.text()}"
    println "Title: ${book.title.text()}"
    println "-------------------------"
}

在这里,我遍历了图书列表并打印了一些数据。

ygya80vv

ygya80vv2#

最好学习XPath和Gpath,如here所述。
但是对于您的示例,如果我们清理XML,请考虑以下情况:

def someInfo = new XmlSlurper().parse("book.xml") 

someInfo.Shops.shop.each { thisShop ->
    // thisShop is the current node <shop> in XML
    println "shop id: " + thisShop."@id"
    thisShop.ctr.each { thisCtr ->
        // thisCtr is the current node <ctr> in XML
        println "country: " + thisCtr.country + " code: " + thisCtr.text()
    }
}

它给出以下输出:

shop id: shop1
country:  code: 01
country:  code: 02
shop id: shop2
country:  code: 03
country:  code: 04

代码is here的工作示例。

tyky79it

tyky79it3#

您可以读取所需的数据,而不会浪费XML解析的资源:

String xml = '''<?xml version="1.0"?> <Someinformation> <catalog>    <book id="bk101">       <author>Gambardella, Matthew</author>       <title>XML Developer's Guide</title>       <genre>Computer</genre>       <price>44.95</price>       <publish_date>2000-10-01</publish_date>       <description>An in-depth look at creating applications        with XML.</description>    </book>    <book id="bk102">       <author>Ralls, Kim</author>       <title>Midnight Rain</title>       <genre>Fantasy</genre>       <price>5.95</price>       <publish_date>2000-12-16</publish_date>       <description>A former architect battles corporate zombies,        an evil sorceress, and her own childhood to become queen        of the world.</description>    </book> </catalog> <Shops>     <shop id ="shop1" index ="1">     <ctr  country="Japan">01</ctr>     <ctr  country="Korea">02</ctr>     </shop>     <shop id ="shop2" index ="2">     <ctr  country="England">03</ctr>     <ctr  country="Germany">04</ctr>     </shop> </Shops> </Someinformation>'''

def shopIds = ( xml =~ /<shop id ="(\w+)" index ="\d">/ ).findAll()*.last()
assert shopIds == ['shop1', 'shop2']

def countries = ( xml =~ /<ctr  country="(\w+)">/ ).findAll()*.last()
assert countries == ['Japan', 'Korea', 'England', 'Germany']

相关问题