我們的數據庫存儲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?
除非你的片段是XHTML,你會想用[Html5.parse(http://scala-tools.org/mvnsites/liftweb-2.4-M4/#net.liftweb.util.Html5解析$)。 – leedm777 2012-02-21 14:47:41
通過解析你自己的HTML,你正在避免[Lift的XSS保護](http://seventhings.liftweb.net/security)。確保在發送回瀏覽器之前清理HTML。 – leedm777 2012-02-21 14:50:44