如何在scala中将xml数组转换为json数据

时间:2015-03-13 11:30:19

标签: xml json scala spray-json

我在下面的数组中有xml数据,即每行对应于数组中的单个元素

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

如何将此xml数组转换为JSON格式?

1 个答案:

答案 0 :(得分:1)

http://scala-tools.org/repo-releases/net/liftweb/

下载lift-json jar

请确保为您的Scala版本获取正确的库,在本文发布时,最新版本位于http://scala-tools.org/repo-releases/net/liftweb/lift-json_2.8.1 /2.3-RC5 /

import net.liftweb.json._
import net.liftweb.json.JsonAST._

val data = xml.XML.loadFile("quotie.xml")
val str = Printer.pretty(render(Xml.toJson(data)))

var out_file = new java.io.FileOutputStream("quotie.json")
var out_stream = new java.io.PrintStream(out_file)

out_stream.print(str)
out_stream.close