Groovy - NekoHTML Sax解析器

时间:2011-08-16 15:03:21

标签: html xml groovy saxparser

我很难使用NekoHTML解析器。 它在URL上工作正常,但是当我想在一个简单的XML测试中进行测试时,它没有正确读取它。

以下是我如何宣布:

def createAndSetParser() { 
  SAXParser parser = new SAXParser()  //Default Sax NekoHTML parser 
  def charset = "Windows-1252"  // The encoding of the page 
  def tagFormat = "upper"    // Ensures all the tags and consistently written, by putting all of them in upper-case. We can choose "lower", "upper" of "match" 
  def attrFormat = "lower"  // Same thing for attributes. We can choose "upper", "lower" or "match" 

  Purifier purifier = new Purifier()     //Creating a purifier, in order to clean the incoming HTML 
  XMLDocumentFilter[] filter = [purifier] //Creating a filter, and adding the purifier to this filter. (NekoHTML feature) 

  parser.setProperty("http://cyberneko.org/html/properties/filters", filter) 
  parser.setProperty("http://cyberneko.org/html/properties/default-encoding", charset) 
  parser.setProperty("http://cyberneko.org/html/properties/names/elems", tagFormat) 
  parser.setProperty("http://cyberneko.org/html/properties/names/attrs", attrFormat) 
  parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true)    // Forces the parser to use the charset we provided to him. 
  parser.setFeature("http://cyberneko.org/html/features/override-doctype", false)    // To let the Doctype as it is. 
  parser.setFeature("http://cyberneko.org/html/features/override-namespaces", false)     // To make sure no namespace is added or overridden. 
  parser.setFeature("http://cyberneko.org/html/features/balance-tags", true) 

  return new XmlSlurper(parser)   // A groovy parser that does not download the all tree structure, but rather supply only the information it is asked for. 
} 

当我在网站上使用它时,它再次正常工作。 有什么猜测为什么我不能在简单的XML文本样本上这样做?

任何帮助大大增加:)

1 个答案:

答案 0 :(得分:2)

我在Groovy控制台上使您的脚本可执行,以便使用Grape从Maven Central Repository中获取所需的NekoHTML库,轻松地尝试它。

@Grapes(
  @Grab(group='net.sourceforge.nekohtml', module='nekohtml', version='1.9.15')
)

import groovy.xml.StreamingMarkupBuilder
import org.apache.xerces.xni.parser.XMLDocumentFilter
import org.cyberneko.html.parsers.SAXParser
import org.cyberneko.html.filters.Purifier

def createAndSetParser() { 
  SAXParser parser = new SAXParser()
  parser.setProperty("http://cyberneko.org/html/properties/filters", [new Purifier()] as XMLDocumentFilter[])
  parser.setProperty("http://cyberneko.org/html/properties/default-encoding", "Windows-1252")
  parser.setProperty("http://cyberneko.org/html/properties/names/elems", "upper")
  parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower")
  parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true)
  parser.setFeature("http://cyberneko.org/html/features/override-doctype", false)
  parser.setFeature("http://cyberneko.org/html/features/override-namespaces", false)
  parser.setFeature("http://cyberneko.org/html/features/balance-tags", true) 
  return new XmlSlurper(parser)
} 

def printResult(def gPathResult) {
  println new StreamingMarkupBuilder().bind { out << gPathResult }
} 

def parser = createAndSetParser()

printResult parser.parseText('<html><body>Hello World</body></html>')
printResult parser.parseText('<house><room>bedroom</room><room>kitchen</room></house>')

当以这种方式执行时,两个printResult语句的结果如下所示,并且可以解释您解析XML字符串的问题,因为它被包装到<html><body>...</body></html>标记中并且丢失了被调用的根标记<house/>

<HTML><tag0:HEAD xmlns:tag0='http://www.w3.org/1999/xhtml'></tag0:HEAD><BODY>Hello World</BODY></HTML>
<HTML><BODY><ROOM>bedroom</ROOM><ROOM>kitchen</ROOM></BODY></HTML>

所有这些都是由您在脚本中启用的http://cyberneko.org/html/features/balance-tags功能引起的。如果我禁用此功能(必须将其显式设置为false,因为它默认为true),结果如下所示:

<HTML><BODY>Hello World</BODY></HTML>
<HOUSE><ROOM>bedroom</ROOM><ROOM>kitchen</ROOM></HOUSE>