如何在Groovy中创建XPath函数 [英] How do I create an XPath function in Groovy
问题描述
- 在运行时接受2个参数(一串XML ,和一个xpath查询)
- 以文本形式返回结果
这可能非常简单但有两个障碍:
- 这需要在groovy中完成
- 没有关于groovy或Java的......
这就像我通过将各种代码一起窃取一样,但现在我' m卡住:
import javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath。*;
builder = DocumentBuilderFactory.newInstance()。newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(xml.bytes));
expr = XPathFactory.newInstance()。newXPath()。compile(expression);
Object result = expr.evaluate(doc,XPathConstants.NODESET)
其中xml和表达式是运行时参数。我现在怎么得到这个结果(作为一个字符串)?
谢谢
import javax.xml.xpath。*
import javax.xml.parsers.DocumentBuilderFactory
def testxml ='''
< records>
< car name =HSV Maloomake =Holdenyear =2006>
< country>澳大利亚< / country>
< record type =speed>速度为271kph的生产皮卡车< / record>
< / car>
< / records>
'''
def processXml(String xml,String xpathQuery){
def xpath = XPathFactory.newInstance()。newXPath()
def builder = DocumentBuilderFactory。 newInstance()。newDocumentBuilder()
def inputStream = new ByteArrayInputStream(xml.bytes)
def records = builder.parse(inputStream).documentElement
xpath.evaluate(xpathQuery,records)
println processXml(testxml,'// car / record / @ type')
看看这个页面(以前是Groovy Docs的一部分),以了解如何遍历XPath查询,这些查询将返回多个结果: b
$ b
http://groovy.jmiguel.eu/ groovy.codehaus.org/Reading+XML+with+Groovy+and+XPath.html
I'm trying to create a function in Groovy that does the following:
- Accepts 2 parameters at runtime (a string of XML, and an xpath query)
- Returns the result as text
This is probably quite straightforward but for two obstacles:
- This has to be done in groovy
- I know next to nothing nothing about groovy or Java…
This is as far as I've got by hacking various bits of code together, but now I'm stuck:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*;
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(xml.bytes));
expr = XPathFactory.newInstance().newXPath().compile(expression);
Object result = expr.evaluate(doc, XPathConstants.NODESET)
where "xml" and "expression" are runtime parameters. How do I get this now to return the result (as a string)?
Thanks
You can do something like this:
import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory
def testxml = '''
<records>
<car name="HSV Maloo" make="Holden" year="2006">
<country>Australia</country>
<record type="speed">Production Pickup Truck with speed of 271kph</record>
</car>
</records>
'''
def processXml( String xml, String xpathQuery ) {
def xpath = XPathFactory.newInstance().newXPath()
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new ByteArrayInputStream( xml.bytes )
def records = builder.parse(inputStream).documentElement
xpath.evaluate( xpathQuery, records )
}
println processXml( testxml, '//car/record/@type' )
Have a look at this page (formerly part of the Groovy Docs) for how to loop over XPath queries that will return multiple results:
http://groovy.jmiguel.eu/groovy.codehaus.org/Reading+XML+with+Groovy+and+XPath.html
这篇关于如何在Groovy中创建XPath函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!