0

我在这里阅读:http: //groovy.codehaus.org/modules/http-builder/doc/get.html

我似乎能够得到

i) 由 NekoHTML 解析的 XMLSlurper 输出,使用:

def http = new HTTPBuilder('http://www.google.com') 
def html = http.get( path : '/search', query : [q:'Groovy'] )

ii) 原始文本使用:

http.get( path : '/search',
          contentType : TEXT,
          query : [q:'Groovy'] ) { resp, reader ->          
  println "response status: ${resp.statusLine}"
  println 'Headers: -----------'
  resp.headers.each { h ->
  println " ${h.name} : ${h.value}"
  }
  println 'Response data: -----'
  System.out << reader
  println '\n--------------------'
}

我遇到了一些麻烦,希望(i) 和 (ii) 在我得到的实际 html 上调试我的 XmlSlurper 代码

有什么建议我可以如何去做吗?

我可以使用 parseString(string) 方法或 parse(reader) 方法轻松地用相关字符串实例化 XmlSlurper 对象,但我似乎无法正确处理 Neko 处理步骤。

有什么提示吗?

谢谢!米莎

4

2 回答 2

3

好的,就是这样。

来自: http: //groovy.codehaus.org/Testing+Web+Applications

def html=http.get(uri:'http://www.google.com',contentType:groovyx.net.http.ContentType.TEXT) { resp,reader ->
  def s=reader.text
  new File("temp.html")<<s
  new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(s)          
}

谢谢!米莎

于 2010-06-03T21:50:15.127 回答
0

不必先转储到文件并从中读取,您可以使用reader.readLines()通过以下实现来实现相同的目的:

def html=http.get(uri:'http://www.google.com',contentType:groovyx.net.http.ContentType.TEXT) { resp,reader ->

  String response = (reader.readLines().join() as String)

  new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText( response)          
}
于 2021-10-13T23:00:36.730 回答