Groovy是用于读取和操作XML文档的DSL,而XMLSlurper是Groovy的库之一。要查找具有给定属性的元素,XMLSlurper可以使用正确的语法,以下是示例代码:
def document = """<?xml version="1.0"?>
<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 and an evil sorceress.</description>
</book>
</catalog>
"""
def books = new XmlSlurper().parse(document)
def authorWithID="bk102"
领取专属 10元无门槛券
手把手带您无忧上云