将XML元素附加到SCALA中的STRING

时间:2017-09-21 18:51:10

标签: xml scala user-defined-functions

我的输入字符串看起来像这样

<p>I want to use a track-bar to change a form's opacity.</p>

<p>This is my code:</p>

 <pre><code>decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
</code></pre>

<p>When I build the application, it gives the following error:</p>

<blockquote>
  <p>Cannot implicitly convert type 'decimal' to 'double'.</p>
</blockquote>

<p>I tried using <code>trans</code> and <code>double</code> but then the 
control doesn't work. This code worked fine in a past VB.NET project. </p>
,While applying opacity to a form should we use a decimal or double value?

我将此字符串传递给spark-scala中的用户定义函数。如您所见,此字符串没有根元素。所以我在这个字符串中附加了一个XML元素。该函数看起来像这样

case class bodyresults(text:String,code:String)

val bodyudf = udf{ (body: String)  =>


    //Appending body tag explicitly to the xml before parsing  
    val xmlElems = xml.XML.loadString(s"""<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE body [<!ENTITY nbsp "&#160;">]><body>${body}</body>""")
    // extract the code inside the req
    val code = (xmlElems \\ "body" \\ "pre" \\ "code").text

    val text = (xmlElems \\ "body").text.replace(s"${code}" ,"" )

    bodyresults(text, code)
}

val posts5 = posts4.withColumn("codetext", bodyudf($"Body") )

当我执行此操作时,我收到此错误

Error: The element type "body" must be terminated by the matching end-tag "</body>"

正如您所看到的,我附加了结束标记。我无法找到导致错误的内容。

任何人都可以让我知道我做错了吗?

0 个答案:

没有答案