在外部存储 GPath 表达式
Store GPath expressions externally
我正在使用 groovy 处理一些 XML,并且我已经通读了以下教程 http://groovy-lang.org/processing-xml.html。我知道如何替换节点,但我想做的是将这些 gpath 表达式存储在一个文件中,并在 运行 时间将它们读入到 "detect" 诸如 urls、数据库连接属性和替换节点 values/attributes 根据需要。
有人知道这是否可行吗?
[编辑]
请求示例
def books = '''
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
<book available="14" id="2">
<title>Catcher in the Rye</title>
<author id="2">JD Salinger</author>
</book>
<book available="13" id="3">
<title>Alice in Wonderland</title>
<author id="3">Lewis Carroll</author>
</book>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
'''
def response = new XmlParser().parseText(books)
response.value.books.book[0].author.replaceNode{
author(id:"99s","Harper Lee")
}
// None of the following will work, but hopefully it shows what I'd like to do
// I'd like to store the path expression as a string
def path = "response.value.books.book[0].author"
//
path.replaceNode{
author(getReplacementValueFromSomeLookup(path) )
}
非常接近这个问题:
def books = '''
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
<book available="14" id="2">
<title>Catcher in the Rye</title>
<author id="2">JD Salinger</author>
</book>
<book available="13" id="3">
<title>Alice in Wonderland</title>
<author id="3">Lewis Carroll</author>
</book>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
'''
def response = new XmlParser().parseText(books)
def path = "ROOT.value.books.book[0].author"
Eval.me('ROOT',response, path).replaceNode{
author('DEMO')
}
println groovy.xml.XmlUtil.serialize(response)
我正在使用 groovy 处理一些 XML,并且我已经通读了以下教程 http://groovy-lang.org/processing-xml.html。我知道如何替换节点,但我想做的是将这些 gpath 表达式存储在一个文件中,并在 运行 时间将它们读入到 "detect" 诸如 urls、数据库连接属性和替换节点 values/attributes 根据需要。
有人知道这是否可行吗?
[编辑] 请求示例
def books = '''
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
<book available="14" id="2">
<title>Catcher in the Rye</title>
<author id="2">JD Salinger</author>
</book>
<book available="13" id="3">
<title>Alice in Wonderland</title>
<author id="3">Lewis Carroll</author>
</book>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
'''
def response = new XmlParser().parseText(books)
response.value.books.book[0].author.replaceNode{
author(id:"99s","Harper Lee")
}
// None of the following will work, but hopefully it shows what I'd like to do
// I'd like to store the path expression as a string
def path = "response.value.books.book[0].author"
//
path.replaceNode{
author(getReplacementValueFromSomeLookup(path) )
}
非常接近这个问题:
def books = '''
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
<book available="14" id="2">
<title>Catcher in the Rye</title>
<author id="2">JD Salinger</author>
</book>
<book available="13" id="3">
<title>Alice in Wonderland</title>
<author id="3">Lewis Carroll</author>
</book>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
'''
def response = new XmlParser().parseText(books)
def path = "ROOT.value.books.book[0].author"
Eval.me('ROOT',response, path).replaceNode{
author('DEMO')
}
println groovy.xml.XmlUtil.serialize(response)