2012-09-06 63 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強制它,但我仍然想知道爲什麼這不起作用。 –