我们的数据库存储HTML 片段,如f.ex. <p>A.</p><p>B.</p>
。我想将数据库中的Html fragements包含在Lift片段中。
为此,我尝试使用XML.loadString()
- 方法将fragement转换为scala.xml.Elem
,但这仅适用于完整有效的XML文档:< / p>
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.
是否可以将(有效)fragements转换为XML?
答案 0 :(得分: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>)
}
答案 1 :(得分:3)
您不需要完整有效的XML文档,但需要一个顶级标记。
正如您所观察到的,以下作品:
XML.loadString("<fragment><p>A</p><p>B</p></fragment>")
然后,您可以存储一系列Elem
s,或将它们包装在自定义标记中,然后使用.descendant
提取序列。