没有合适的资源?快使用搜索试试~ 我知道了~
首页Scala xml解析全解教程
资源详情
资源推荐
1
ScalaTest - http://scalatest.org
2
Akka - http://akka.io/
3
Play Framework - http://www.playframework.com/
4
scala.xml API - http://www.scala-lang.org/files/archive/nightly/docs/xml/
5
scala-xml GitHub repository - https://github.com/scala/scala-xml/
XML Processing in Scala
Dino Fancellu
Felstar Ltd
<
dino@felstar.com>
William Narmontas
Apt Elements Ltd
<
william@scalawilliam.com>
Abstract
Scala is an established static- and strongly-typed functional
and object-oriented scalable programming language for the
JVM with seamless Java interoperation.
Scala and its ecosystem are used at LinkedIn, Twitter,
Morgan Stanley among many companies demanding
remarkable time to market, robustness, high performance
and scalability.
is paper shows you Scala's strong native XML support,
powerful XQuery-like constructs, hybrid processing via
XQuery for Scala, and increased XML processing
performance. You will learn how you can benefit from
Scala’s practicality in a commercial setting, ultimately
increasing your productivity.
Keywords: Scala, XML, XQuery, XSLT, XQJ, Java,
Processing
1. Introduction
Programming style: Scala’s immutability, functional
programming, first-class XML make it rather similar to
XQuery. Scala’s for-comprehensions were inspired by
Philip Wadler from his work with XQuery.
[1]
Ecosystem: Scala’s seamless Java interoperation gives
you access to all of Java’s libraries, the JVM [2] and many
outstanding Scala libraries
1
2
3
.
Scalability: Scala’s scalability and design negate the
need for design patterns in solving a language’s design
flaws. It is everything that Java should have been.
XML handling: Scala’s XML handling includes the
standard XML types such as Element, Attribute, Node. It
also includes the NodeSeq type which extends Seq[Node]
(a sequence of nodes), meaning that all of Scala’s
collections functionality for sequences is available for
XML types. e key Scala XML documentation can be
found at its author’s Burak Emir's Scala XML book
[3],
scala.xml API
4
and scala-xml GitHub repository
5
.
2. Five minutes to understanding
Scala
is paper covers a relevant selection of Scala’s
capabilities. ere are many great resources to learn
about traits, partial functions, case classes, etc. We will
cover the necessary essentials for this paper. See Scala
crash course
[4] and a selected presentation [5] for
detailed walk-throughs.
Like with XQuery and other functional
programming languages we recommend programming
Scala in an immutable fashion, although Scala allows you
to program in an Object Oriented fashion or hybrid of
the two, making it especially suited to migrating from a
Java code base.
Scala’s types are static, strong and mostly inferred, to
the extent that it can feel like a scripting language [6] .
Your IDE and Scala’s compiler will inform you of your
program’s correctness very early on - including XML
well-formedness.
Scala’s ‘implicits’ enable you to define new methods
on values in a limited scope. With implicits and type
inference your code becomes very compact [7] [8]. In
fact, this paper displays types only for the sake of clarity.
doi:10.14337/XMLLondon14.Narmontas01
Page 63 of 162
Scala is about expressions, not statements. e last
expression in a block of expressions is the return value.
e same applies to if-statements and try-catch.
Scala is best used from within IntelliJ IDEA and
Eclipse with the Scala IDE plug-in.
[9]
2.1. Values and functions
Scala & XQuery:
• def fun(params): type similar to
declare function local:fun(params): type
• val xyz = {expression} similar to
let $xyz := {expression}
Functions can be passed around easily. Example:
def incrementedByOne(x: Int) = x + 1
(1 to 5).map(incrementedByOne)
Vector(2, 3, 4, 5, 6)
is example however can be slimmed down to
(1 to 5).map(x => x + 1)
Vector(2, 3, 4, 5, 6)
Where x => x + 1 is an anonymous (lambda) function.
It can be slimmed down further to
(1 to 5).map(_+1)
Vector(2, 3, 4, 5, 6)
Scala’s collections, such as lists, sets and maps come in
mutable and immutable flavours [10] . ey will be used
throughout the examples.
2.2. Strings and string interpolation
e triple double-quote syntax negates escaping of
double-quotes in string literals. E.g.
val title = """An introduction to "Scala""""
Scala supports string interpolation [11] similar to that in
PHP, Perl and CoffeeScript - with the ‘s’ modifier:
val language = "Scala"
val interpolatedTitle =
s"""An introduction to "$language""""
String interpolation turns $language into
${language.toString}.
Scala’s triple-quoted strings may be multi-line, as
shown in the examples section.
2.3. Named parameters
Where further clarity for method calls is needed, you can
use named parameters:
def makeLink(url: String, text: String) =
s"""<a href="$url">$text</a>"""
makeLink(text = "XML London 2014",
url="http://www.xmllondon.com/")
<a href="http://www.xmllondon.com/">
XML London 2014</a>
2.4. For-comprehensions
For-comprehensions [12] will be familiar to a
programmer who has used Python, LINQ, XQuery,
Ruby, Haskell, F#, Erlang, Clojure.
You can rewrite the previous example
(1 to 5).map(x => x + 1) as a for-comprehension:
for ( x <- (1 to 5) ) yield x + 1
Vector(2, 3, 4, 5, 6)
ese comprehensions yield results by iterating over
multiple collections:
val software = Map(
"Browser" -> Set("Firefox", "Chrome",
"Internet Explorer"),
"Office Suite" -> Set(
"Google Drive", "Microsoft Office",
"Libre Office")
)
for { (softwareKind, programs) <- software
program <- programs
if program endsWith "e"
} yield s"$softwareKind: $program"
List(Browser: Chrome, Office Suite: Google Drive,
Office Suite: Microsoft Office,
Office Suite: Libre Office)
Inside a for-comprehension, Scala and XQuery once
again share similarities:
• x <- {expression} similar to
for $x in {expression}
• if {condition} similar to
where {condition}
• abc = {expression} similar to
let $abc := {expression}
• yield {expression} similar to
return {expression}
Page 64 of 162
XML Processing in Scala
1
scala-xml library GitHub - https://github.com/scala/scala-xml/
3. Scala's strong native XML
support
Unlike in Java, XML is a first class citizen in Scala and
can be used as a native data type.
e scala.xml library source code is available on
GitHub.
1
3.1. Basic Inline XML
XML literals can be embedded directly in code with
curly braces.
val title = "XML London 2014"
val xmlTree = <div>
<p>Welcome to <em>{title}</em>!</p>
</div>
Serializing this XML structure works as expected:
xmlTree.toString
<div>
<p>Welcome to <em>XML London 2014</em>!</p>
</div>
ese XML literals are checked for well formedness at
compile time or even in your IDE reducing errors.
Curly braces can be escaped with double braces. e.g.
val squiggles = <root>I like {{squiggles}}</root>
<root>I like {squiggles}</root>
3.2. Reading
Scala can load XML from Java’s File, InputStream,
Reader, String using the scala.xml.XML object. Here is
an XML document in String form:
val pun =
"""<pun rating="extreme">
| <question>Why do CompSci students need
|glasses?</question>
| <answer>To C#<!--
|C# is a Microsoft's programming language
|-->.</answer>
|</pun>""".stripMargin
Loading an XML document from a String gives us a
node:
scala.xml.XML.loadString(pun)
<pun rating="extreme">
<question>Why do CompSci students need
glasses?</question>
<answer>To C#.</answer>
</pun>
When you need XML comments use the
ConstructingParser [13] :
scala.xml.parsing.ConstructingParser
.fromSource(scala.io.Source.fromString(pun),
preserveWS = true).document().docElem
<pun rating="extreme">
<question>Why do CompSci students need
glasses?</question>
<answer>To C#<!--
C# is a Microsoft's programming language
-->.</answer>
</pun>
3.2.1. Look ups and XPath alternatives
Scala has its own XPath-like methods for querying from
XML trees
val listOfPeople = <people>
<person>Fred</person>
<person>Ron</person>
<person>Nigel</person>
</people>
listOfPeople \ "person"
NodeSeq(<person>Fred</person>,
<person>Ron</person>, <person>Nigel</person>)
Wildcard is similar
listOfPeople \ "_"
NodeSeq(<person>Fred</person>,
<person>Ron</person>, <person>Nigel</person>)
Looking for descendants
val fact = <fact type="universal">
<variable>A</variable> = <variable>A</variable>
</fact>
fact \\ "variable"
NodeSeq(<variable>A</variable>,
<variable>A</variable>)
Querying attributes is similar
fact \ "@type"
: scala.xml.NodeSeq = universal
fact \@ "type"
: String = universal
Page 65 of 162
XML Processing in Scala
剩余12页未读,继续阅读
笨小孩Andy
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功