2011-08-16 74 views
4

我很難與我的NekoHTML解析器。 它在URL上工作正常,但是當我想測試一個簡單的XML測試時,它不會正確讀取它。Groovy - NekoHTML Sax解析器

這是我如何把它聲明:

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文本樣本?

任何幫助非常apreciated :)

+5

失敗時它有什麼作用?有沒有堆棧跟蹤?什麼是它失敗的XML文檔的例子?請提供更多信息。 –

+0

抱歉回答這麼晚了,謝謝你的回答。 解析不會崩潰,也不會寫入堆棧跟蹤。 這只是不正確解析。 例如,如果我給下面的示例: 臥室 廚房 文件的路徑(路徑相當於文檔節點的)實際上是文本「臥室」 ... 因此,我的問題是這,它不正確地初始化解析,阻止我做我想做的事。 如果你有什麼想法可能是錯誤的......我在聽:) –

回答

2

我做了Groovy的控制檯腳本執行來嘗試一下輕鬆地用葡萄來從Maven的中央倉庫所需的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 -statements的結果看起來如下圖所示,可以解釋你的問題解析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>