如何在 Scala 中将 xml 数组转换为 json 数据
How to convert xml array to json data in scala
我有如下数组中的 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 格式?
从 http://scala-tools.org/repo-releases/net/liftweb/
下载 lift-json jar
一定要为你的 Scala 版本获取合适的库,在这个时候 post 最新的位于 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
我有如下数组中的 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 格式?
从 http://scala-tools.org/repo-releases/net/liftweb/
下载 lift-json jar一定要为你的 Scala 版本获取合适的库,在这个时候 post 最新的位于 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