2012-02-21 2 views
1

우리의 데이터베이스는 과 같은 f.ex의 HTML을 저장합니다. <p>A.</p><p>B.</p>. 데이터베이스의 HTML 조각을 리프트 스 니펫에 포함하고 싶습니다.스칼라 : 파편 HTML- 조각

이를 위해, 나는 scala.xml.Elem로의 fragment를 변환 할 XML.loadString() -method를 사용하려고했으나 전체 유효한 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을하지 않는 한, 당신은 http://scala-tools.org/mvnsites/liftweb-2.4-M4/#net.liftweb.util.Html5 ([Html5.parse]와 구문 분석 할 것 $). – leedm777

+1

그리고 자신의 HTML을 파싱하면 [리프트의 XSS 보호] (http://seventhings.liftweb.net/security)를 피할 수 있습니다. 브라우저로 다시 보내기 전에 HTML을 살균하십시오. – leedm777

답변

5

Lift를 사용 중이므로 lift:children에 XML을 래핑 할 수 있습니다. 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

3

전체 유효한 XML 문서가 필요하지 않지만 단일 최상위 태그가 필요합니다.

당신이 관찰

다음 작품 : 당신은 다음 Elem s의 순서를 저장하거나 사용자 정의 태그에서 그들을 감싸고 .descendant를 사용하여 순서를 추출 할 수있는 하나

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

.

+0

이것은 또한 작동합니다 - 고마워요. 그러나 간단한 Children-Snippet-workaround를 사용할 것입니다. – Sonson