2012-09-06 66 views
0

考虑下面的HTML片段,我需要提取的内容属性的文本为meta标签与属性name等于descriptionmeta标签与属性property等于og:title。我试过Groovy: Correct Syntax for XMLSlurper to find elements with a given attribute上显示的内容,但它在Groovy 1.8.6中看起来并不一样。选择使用的XmlSlurper像WHERE子句

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#"> 
    <head> 
    <meta http-equiv="X-UA-Compatible" content="IE=8" /> 
    <meta property="fb:admins" content="100003979125336" /> 
    <meta name="description" content="Shiny embossed blue butterfly" /> 
    <meta name="keywords" content="Fancy That Blue Butterfly Platter" /> 
    <meta property="og:title" content="Fancy That Blue Butterfly Platter" /> 

有没有一种干净的方式来检索这些与GPath?

回答

0

这适用于常规2.0.1 - 我没有1.8.6得心应手的时刻:

def slurper = new XmlSlurper() 
File xmlFile = new File('sample.xml') 
def xml = slurper.parseText(xmlFile.text) 
println 'description = ' + xml.head.children().find{it.name() == 'meta' && [email protected] == 'description'}[email protected] 
println 'og:title = ' + xml.head.children().find{it.name() == 'meta' && [email protected] == 'og:title'}[email protected] 
+0

嗯,这让我空字符串1.8.6。我使用indexOf强制它,但我仍然想知道为什么这不起作用。 –