Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy XMLSlurper-搜索独立节点_Groovy_Xml Parsing_Xmlslurper - Fatal编程技术网

Groovy XMLSlurper-搜索独立节点

Groovy XMLSlurper-搜索独立节点,groovy,xml-parsing,xmlslurper,Groovy,Xml Parsing,Xmlslurper,我需要用Groovy的XMLSlurper找到一个特定的节点。条件应该是子节点的文本/值必须匹配。在下面的示例中,我想搜索一个book节点,其中年份为“2003”,价格为“39.95” <bookstore name="Store A"> <employee> <id>546343</id> <name>Dustin Brown</name> </employee> <e

我需要用Groovy的XMLSlurper找到一个特定的节点。条件应该是子节点的文本/值必须匹配。在下面的示例中,我想搜索一个book节点,其中年份为“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>

546343
布朗
547547
丽莎·丹顿
日常意大利语
吉娅达·德·劳伦蒂斯
2005
30
哈利·波特
J K.罗琳
2005
29.99
学习XML
埃里克·T·雷
2003
39.95
...
给定:

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>'''

这里有另一种实现同样目标的方法

请注意,通过如下所示的添加,用户可以轻松地更改/添加附加的
条件

def queryData = [[<element>, <operator>, <element value>], [<element>, <operator>, <element value>], ...]
基本上,它基于
queryData
创建一个
closure
,并将其传递给
findAll

getQuery
closure将查询构建到上述条件列表中,如下所示:

{ it -> it.year.text() == '2003' && it.price.text() <= '39.95' }
{it->it.year.text()
def queryData = [['year','EQ', '2003'], ['price', 'LE', '39.95']]
{ it -> it.year.text() == '2003' && it.price.text() <= '39.95' }