如何使用XSLT将我的Scala XML对象(类Elem)转换为HTML?

时间:2021-12-10 19:42:39

I have a small piece of code that, given the paths of my XML and XSLT files, outputs an HTML file in the provided path. It goes like this:

我有一小段代码,根据我的XML和XSLT文件的路径,在提供的路径中输出一个HTML文件。它是这样的:

  try {
       val tFactory: TransformerFactory = TransformerFactory.newInstance
       val transformer: Transformer = tFactory.newTransformer(new StreamSource("<PATH TO XSL>"))
       transformer.transform(new StreamSource("<PATH TO XML>"), new StreamResult(new FileOutputStream("PATH TO HTML")))
 } catch {
       case e: Exception => e.printStackTrace
 }

Now, instead of an XML file as an input, I want to input a Scala Elem object. How do I make that possible?

现在,我想输入一个Scala Elem对象,而不是XML文件作为输入。我该如何做到这一点?

1 个解决方案

#1


0  

You need to create string from your element.

您需要从元素创建字符串。

val xml = <elem/>
transformer.transform(new StreamSource(new StringReader(xml.toString())), new StreamResult(writer))

#1


0  

You need to create string from your element.

您需要从元素创建字符串。

val xml = <elem/>
transformer.transform(new StreamSource(new StringReader(xml.toString())), new StreamResult(writer))