2012-02-21 63 views
1

我們的數據庫存儲HTML 片段像f.ex. <p>A.</p><p>B.</p>。我想將來自數據庫的Html分段包含到Lift代碼片段中。Scala:解析HTML片段

爲了做到這一點,我試圖用XML.loadString() - 方法的fragement轉換成scala.xml.Elem,但這隻適用於有效XML的文檔:

import scala.xml.XML 
@Test 
def doesnotWork() { 
    val result = XML.loadString("<p>A</p><p>B</p>") 
    assert(result === <p>A</p><p>B</p>) 
} 

@Test 
def thisWorks() { 
    val result = XML.loadString("<test><p>A</p><p>B</p></test>") 
    assert(result === <test><p>A</p><p>B</p></test>) 
} 

測試doesnotWork結果在例外:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 10; The markup in the document following the root element must be well-formed. 

是否可以將正好(有效)的分數轉換爲XML?

+1

除非你的片段是XHTML,你會想用[Html5.parse(http://scala-tools.org/mvnsites/liftweb-2.4-M4/#net.liftweb.util.Html5解析$)。 – leedm777 2012-02-21 14:47:41

+1

通過解析你自己的HTML,你正在避免[Lift的XSS保護](http://seventhings.liftweb.net/security)。確保在發送回瀏覽器之前清理HTML。 – leedm777 2012-02-21 14:50:44

回答

5

由於您使用的是Lift,因此可以將您的XML封裝在lift:children中作爲解決方法。 Children片段只是返回元素的子元素;對於包裝需要解析的碎片非常有用。

@Test 
def thisAlsoWorks() { 
    val result = XML.loadString("<lift:children><p>A</p><p>B</p></lift:children>") 
    assert(result === <lift:children><p>A</p><p>B</p></lift:children>) 
} 
+0

謝謝!我不知道孩子的片段。 – Sonson 2012-02-21 15:58:33

3

您不需要完整的有效XML文檔,但您確實需要一個頂級標記。

當你觀察到的,下面的工作:

XML.loadString("<fragment><p>A</p><p>B</p></fragment>") 

然後,您可以任意存儲Elem個序列,或自定義標籤將它們包裝並提取使用.descendant序列。

+0

這也適用 - 謝謝。但我會使用簡單的Children-Snippet解決方法。 – Sonson 2012-02-21 16:00:11