Groovy XMLSlurper - 搜索特定节点

时间:2017-02-14 09:38:55

标签: groovy xml-parsing xmlslurper

我需要使用Groovy的XMLSlurper找到一个特定的节点。条件应该是子节点的文本/值必须匹配。在下面的示例中,我想搜索年份为“2003”且价格为“39 .95”的图书节点。

<bookstore name="Store A">
  <employee>
      <id>546343</id>
      <name>Dustin Brown</name>
  </employee>
  <employee>
      <id>547547</id>
      <name>Lisa Danton</name>
  </employee>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
<bookstore name="Store B">
...
</bookstore>

2 个答案:

答案 0 :(得分:2)

假设:

def xml = '''<stores>
  <bookstore name="Store A">
      <employee>
          <id>546343</id>
          <name>Dustin Brown</name>
      </employee>
      <employee>
          <id>547547</id>
          <name>Lisa Danton</name>
      </employee>
      <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
      </book>
      <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      <book category="web">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>
  </bookstore>
  <bookstore name="Store B">
  </bookstore>
</stores>'''

然后

new XmlSlurper().parseText(xml).bookstore.book.findAll { it.year == '2003' && it.price == '39.95' }

答案 1 :(得分:0)

这是实现同样目标的另一种方式。

请注意,用户可以通过添加轻松更改/添加其他with open(fileLocation,"r+") as openFile: for line in openFile: if line.strip().startswith("objectName:"): line = re.sub(initialName.replace(".qml",""),camelCaseName.replace(".qml",""),line) print line openFile.write(line) openFile.close() 条件,如下所示

and

运营商可以是以下人员之一:

  • def queryData = [[<element>, <operator>, <element value>], [<element>, <operator>, <element value>], ...] 等于
  • EQ小于或等于
  • LE大于或等于
  • GE for than
  • GT少于
  • LT不等于

例如:

NE

基本上它会根据def queryData = [['year','EQ', '2003'], ['price', 'LE', '39.95']] 创建closure并将其传递给queryData

findAll 关闭将查询构建为上述条件列表

getQuery

我觉得一些新人尝试groovy会更容易使用上面的列表而不是上面的闭包,这是动态构建的。

以下是脚本:

{ it -> it.year.text() == '2003' && it.price.text() <= '39.95' }

您可以快速尝试 Demo

另请注意,它目前只有​def xml = """<stores> <bookstore name="Store A"> <employee> <id>546343</id> <name>Dustin Brown</name> </employee> <employee> <id>547547</id> <name>Lisa Danton</name> </employee> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> <bookstore name="Store B"> </bookstore> </stores>""" //You may just add additional conditions into below list def queryData = [['year','EQ', '2003'], ['price', 'LE', '39.95']] enum Operator { EQ('=='), LE('<='), GE('>='), GT('>'), LT('<'), NE('!=') def value Operator(String value){ this.value = value } def getValue(){ value } } def getQuery = { list -> def sb = new StringBuffer('{ it -> ') list.eachWithIndex { sublist, index -> index == 0 ?: sb.append(' && ') Operator operator = sublist[1] sb.append("it.${sublist[0]}.text() ${operator.value} '${sublist[2]}'") } def query = sb.append(' }').toString() println "Query formed is : ${query}" def sh = new GroovyShell() sh.evaluate(query) } def getBooks = { stores, closure -> stores.'**'.findAll { closure(it) } ?: 'Could not find matching book' } def stores = new XmlSlurper().parseText(xml) def result = getBooks(stores, getQuery(queryData)) println result 个条件,不支持and