正则表达式以查找XML标记

时间:2014-12-10 16:59:21

标签: regex xml groovy

我有以下格式的XML

<Employee>
<ID>..</ID>
<E-mail>..</E-mail>
 ...
<custom_1>..</custom_1>
<custom_2>..</custom_2>
 <custom_3>..</custom_3>
 </Employee>

我的要求是找到XML中以&#34; custom _ *&#34;开头的所有标签。我使用的是Groovy 因此做这样的事情(在Groovy中使用XMLParse)

有人可以在这里指导我。

谢谢, VIPIN

1 个答案:

答案 0 :(得分:0)

尝试这样做:

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

def testxml = '''
                <Employee>
                  <ID>..</ID>
                  <E-mail>..</E-mail>
                  <custom_1>foo</custom_1>
                  <custom_2>bar</custom_2>
                  <custom_3>base</custom_3>
                </Employee>
  '''

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, XPathConstants.NODESET )
}

def result = processXml( testxml, '//*[starts-with(name(), "custom")]' )
result.length.times{
    println result.item(it).textContent
}