タスク: HTML - Scala のパーサー。私はscalaにかなり慣れていません。
これまでのところ、ランダムな html ドキュメントを解析するための小さなパーサーを Scala で作成しました。
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」が機能しないことです。基本的に、1 つのノードのコンテンツを正確に取得したいと考えています。または、一致せずにそれを達成する別の方法はありますか?
loadxml-function の「フィード」は、解析に適した形式を返しますか、それともそれを達成するためのより良い方法はありますか? フィードはルート ノードを返しますよね?
前もって感謝します