如何在Groovy中创建XPath函数

时间:2021-12-01 09:18:04

I'm trying to create a function in Groovy that does the following:

我正在尝试用Groovy创建一个函数,它的作用是:

  1. Accepts 2 parameters at runtime (a string of XML, and an xpath query)
  2. 在运行时接受2个参数(XML字符串和xpath查询)
  3. Returns the result as text
  4. 以文本形式返回结果

This is probably quite straightforward but for two obstacles:

这可能很简单,但有两个障碍:

  1. This has to be done in groovy
  2. 这必须在groovy中完成
  3. I know next to nothing nothing about groovy or Java…
  4. 我对groovy或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)?

其中“xml”和“表达式”是运行时参数。如何让这个返回结果(作为字符串)?

Thanks

谢谢

2 个解决方案

#1


14  

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:

请查看这个页面(以前是Groovy文档的一部分),了解如何对将返回多个结果的XPath查询进行循环:

http://groovy.jmiguel.eu/groovy.codehaus.org/Reading+XML+with+Groovy+and+XPath.html

Groovy http://groovy.jmiguel.eu/groovy.codehaus.org/Reading + XML + + +和+ XPath.html

#2


0  

This was what I eventually settled for, which should work for my purposes:

这就是我最终的目标,对我的目标应该是有效的:

import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory

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
  def nodes       = xpath.evaluate( xpathQuery, records, XPathConstants.NODESET )
  nodes.collect { node -> node.textContent }

}

processXml( xml, query )

#1


14  

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:

请查看这个页面(以前是Groovy文档的一部分),了解如何对将返回多个结果的XPath查询进行循环:

http://groovy.jmiguel.eu/groovy.codehaus.org/Reading+XML+with+Groovy+and+XPath.html

Groovy http://groovy.jmiguel.eu/groovy.codehaus.org/Reading + XML + + +和+ XPath.html

#2


0  

This was what I eventually settled for, which should work for my purposes:

这就是我最终的目标,对我的目标应该是有效的:

import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory

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
  def nodes       = xpath.evaluate( xpathQuery, records, XPathConstants.NODESET )
  nodes.collect { node -> node.textContent }

}

processXml( xml, query )