Groovy 语法 - 什么是“**”xml 方法之类的东西?
Groovy syntax - what is the '**' xml method-like-thingy?
我在这里找到了一些有用的 xml 操作代码:
http://www.groovy-lang.org/processing-xml.html
它给出了以下有用的示例:
def response = new XmlSlurper().parseText(books)
def titles = response.'**'.findAll{ node-> node.name() == 'title' }*.text()
我知道这是一个通配符约定,但是“**”字符串究竟是如何指示 findAll 方法搜索每个节点的?还有哪些其他字符串会做有用的事情?这在某处记录了吗?
这是 depthFirst()
的快捷方式。请参阅 GPathResult#getProperty(String) 的 API 文档:
Returns the specified Property of this GPathResult.
Realizes the follow shortcuts:
'..' for parent()
'*' for children()
'**' for depthFirst()
'@' for attribute access
关于 getProperty 是如何做到的,这里是来自 GPathResult 的代码:
public Object getProperty(final String property) {
if ("..".equals(property)) {
return parent();
} else if ("*".equals(property)) {
return children();
} else if ("**".equals(property)) {
return depthFirst();
} else if (property.startsWith("@")) {
if (property.indexOf(":") != -1) {
final int i = property.indexOf(":");
return new Attributes(this, "@" + property.substring(i + 1), property.substring(1, i), this.namespaceTagHints);
} else {
return new Attributes(this, property, this.namespaceTagHints);
}
} else {
if (property.indexOf(":") != -1) {
final int i = property.indexOf(":");
return new NodeChildren(this, property.substring(i + 1), property.substring(0, i), this.namespaceTagHints);
} else {
return new NodeChildren(this, property, this.namespaceTagHints);
}
}
}
我一直期待看到某种使用 missingProperty 或其他东西的元编程 hackery,但这里不需要。对 response.'**'
的调用被视为访问 属性,使用传入的“**”作为参数调用 getProperty。
这是 depthFirst
的别名
我想不出在其他任何地方发生这种事情 groovy,XML 解析是一个特例
我在这里找到了一些有用的 xml 操作代码: http://www.groovy-lang.org/processing-xml.html
它给出了以下有用的示例:
def response = new XmlSlurper().parseText(books)
def titles = response.'**'.findAll{ node-> node.name() == 'title' }*.text()
我知道这是一个通配符约定,但是“**”字符串究竟是如何指示 findAll 方法搜索每个节点的?还有哪些其他字符串会做有用的事情?这在某处记录了吗?
这是 depthFirst()
的快捷方式。请参阅 GPathResult#getProperty(String) 的 API 文档:
Returns the specified Property of this GPathResult.
Realizes the follow shortcuts:
'..' for parent() '*' for children() '**' for depthFirst() '@' for attribute access
关于 getProperty 是如何做到的,这里是来自 GPathResult 的代码:
public Object getProperty(final String property) {
if ("..".equals(property)) {
return parent();
} else if ("*".equals(property)) {
return children();
} else if ("**".equals(property)) {
return depthFirst();
} else if (property.startsWith("@")) {
if (property.indexOf(":") != -1) {
final int i = property.indexOf(":");
return new Attributes(this, "@" + property.substring(i + 1), property.substring(1, i), this.namespaceTagHints);
} else {
return new Attributes(this, property, this.namespaceTagHints);
}
} else {
if (property.indexOf(":") != -1) {
final int i = property.indexOf(":");
return new NodeChildren(this, property.substring(i + 1), property.substring(0, i), this.namespaceTagHints);
} else {
return new NodeChildren(this, property, this.namespaceTagHints);
}
}
}
我一直期待看到某种使用 missingProperty 或其他东西的元编程 hackery,但这里不需要。对 response.'**'
的调用被视为访问 属性,使用传入的“**”作为参数调用 getProperty。
这是 depthFirst
我想不出在其他任何地方发生这种事情 groovy,XML 解析是一个特例