2009-11-08 8 views
18

HTML DOMドキュメントをScalaにロードするにはどうすればよいですか? xmlnsタグをロードしようとすると、XMLシングルトンにエラーが発生しました。ScalaとHTMLの解析

import java.net._ 
import java.io._ 
import scala.xml._ 

object NetParse { 

    import java.net.{URLConnection, URL} 
    import scala.xml._ 

    def netParse(sUrl: String): Elem = { 
     var url = new URL(sUrl) 
     var connect = url.openConnection 

     XML.load(connect.getInputStream) 
    } 
} 

最後に、私は解決策を見つけました! - (2.7.0は致命的なバグがあります)動作するようにスカラ座2.7.7以上が必要です: How-to-use-TagSoup-with-Scala-XML

答えて

15
+2

今日このページを表示すると、ほとんどのコード例が表示されません。ここには、元のコンテンツがすべて含まれているバージョンへのリンクがあります。http://web.archive.org/web/20111121010724/http://www.hars.de/2009/01/html-as-xml-in- scala.html –

+0

あなたのプロジェクトにこのライブラリを手軽に入手したい人は、[http://mvnrepository.com/artifact/org.ccil.cowan.tagsoup/tagsoup/1.2.1](http://mvnrepository) .com/artifact/org.ccil.cowan.tagsoup/tagsoup/1.2.1)。 – icl7126

6

ではなくscala.xml.parsing.XhtmlParserを使用してみてくださいに役立つことがあります。

+1

このソリューションは「タグスープ」では機能しません。正式なXHTMLだけが正常に解析されます。したがって、基本的には、標準のHTMLエンティティを追加し、scala.xml.XML.load *と比較してCDATAブロックを保持するように見えます。 (私の場合、これは私が必要としていたものなので、ええ!) –

2
/* 
Copyright (c) 2008 Florian Hars, BIK Aschpurwis+Behrens GmbH, Hamburg 
Copyright (c) 2002-2008 EPFL, Lausanne, unless otherwise specified. 
All rights reserved. 

This software was developed by the Programming Methods Laboratory of the 
Swiss Federal Institute of Technology (EPFL), Lausanne, Switzerland. 

Permission to use, copy, modify, and distribute this software in source 
or binary form for any purpose with or without fee is hereby granted, 
provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright 
    notice, this list of conditions and the following disclaimer. 

2. Redistributions in binary form must reproduce the above copyright 
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution. 

3. Neither the name of the EPFL nor the names of its contributors 
    may be used to endorse or promote products derived from this 
    software without specific prior written permission. 


THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
SUCH DAMAGE. 
*/ 

package tagsoup 

import org.xml.sax.InputSource 
import javax.xml.parsers.SAXParser 
import org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl 
import scala.xml.parsing.FactoryAdapter 
import scala.xml._ 

class TagSoupFactoryAdapter extends FactoryAdapter { 

    val parserFactory = new SAXFactoryImpl 
    parserFactory.setNamespaceAware(false) 

    val emptyElements = Set("area", "base", "br", "col", "hr", "img", 
         "input", "link", "meta", "param") 

    /** Tests if an XML element contains text. 
    * @return true if element named <code>localName</code> contains text. 
    */ 
    def nodeContainsText(localName: String) = !(emptyElements contains localName) 

    /** creates a node. 
    */ 
    def createNode(pre:String, label: String, attrs: MetaData, 
      scpe: NamespaceBinding, children: List[Node]): Elem = { 
    Elem(pre, label, attrs, scpe, children:_*); 
    } 

    /** creates a text node 
    */ 
    def createText(text:String) = 
    Text(text); 

    /** Ignore Processing Instructions 
    */ 
    def createProcInstr(target: String, data: String) = Nil 

    /** load XML document 
    * @param source 
    * @return a new XML document object 
    */ 
    override def loadXML(source: InputSource) = { 
    val parser: SAXParser = parserFactory.newSAXParser() 

    scopeStack.push(TopScope) 
    parser.parse(source, this) 
    scopeStack.pop 
    rootElem 
    } 

} 

How-to-use-TagSoup-with-Scala-XML

5

私はちょうどからのScala 2.8.1でこの答えを使用しようと作業を使用して終了している:私は必要面白い少しだった

http://www.hars.de/2009/01/html-as-xml-in-scala.html

val parserFactory = new org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl 
val parser = parserFactory.newSAXParser() 
val source = new org.xml.sax.InputSource("http://www.scala-lang.org") 
val adapter = new scala.xml.parsing.NoBindingFactoryAdapter 
adapter.loadXML(source, parser) 
+1

これは私のためにも機能します。しかし、生のHTMLを入力ソースに変換できるようにしたいので、 'val source ='行か 'adapter.loadXML'セクションのどちらかで行います。 'adapter.loadString(" ... ")'を試しましたが、不正な形式のコンテンツで失敗します。何か案は? – jbnunn

2

スカラ・スクレーパー

私はあなたがエレガントに、このようにHTMLを解析することができますScala Scraperをお勧めします:

// Parse elements from files, URLs or plain strings 
val browser = JsoupBrowser() 
val doc = browser.parseFile("core/src/test/resources/example.html") 
val doc2 = browser.get("http://example.com") 
val doc3 = browser.parseString("<html><h1>parse me</h1></html>") 

// Extract the text inside the element with id "header" 
doc >> text("#header") 

// Extract the <span> elements inside #menu 
val items = doc >> elementList("#menu span") 

// From each item, extract all the text inside their <a> elements 
items.map(_ >> allText("a")) 

例としては、Scalaのスクレーパーのreadmeから取られています。