任务:HTML - Scala 中的解析器。我对 scala 很陌生。
到目前为止:我已经用 Scala 编写了一个小 Parser 来解析随机的 html 文档。
import scala.xml.Elem
import scala.xml.Node
import scala.collection.mutable.Queue
import scala.xml.Text
import scala.xml.PrettyPrinter
object Reader {
def loadXML = {
val parserFactory = new org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
val parser = parserFactory.newSAXParser()
val source = new org.xml.sax.InputSource("http://www.randomurl.com")
val adapter = new scala.xml.parsing.NoBindingFactoryAdapter
val feed = adapter.loadXML(source, parser)
feed
}
def proc(node: Node): String =
node match {
case <body>{ txt }</body> => "Partial content: " + txt
case _ => "grmpf"
}
def main(args: Array[String]): Unit = {
val content = Reader.loadXML
Console.println(content)
Console.println(proc(content))
}
}
问题是“proc”不起作用。基本上,我想准确获取一个节点的内容。还是有另一种方法可以在不匹配的情况下实现这一目标?
loadxml 函数中的“提要”是否为我提供了正确的解析格式,还是有更好的方法来实现这一点?Feed 还给我根节点,对吧?
提前致谢