Scala读XML文件

Scala读XML文件

在Scala2.10版本后,XML,Swing,parser-combinators模块不在包含在Scala包中,如果需要使用需要手动添加依赖
这里是用Maven构建项目。
scalaBinaryVersion 指的是你的版本的大号,比如你用的是2.11.8 scalaBinaryVersion即为2.11
详情可以参考https://github.com/scala/scala-module-dependency-sample

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-xml_${scalaBinaryVersion}</artifactId>
<version>1.0.6</version>
</dependency>

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.anthony

/**
* @ Description:
* @ Date: Created in 06:37 11/07/2018
* @ Author: Anthony_Duan
*/


import scala.xml.XML

object readXML extends App {


val xml = XML.load("/Users/duanjiaxing/IdeaProjects/scala-train/src/main/resources/hive-site.xml")
// println(xml)
// val xm2 = XML.load(this.getClass.getClassLoader.getResource("hive-site.xml"))
// val xm3 = XML.load(new FileInputStream("/Users/duanjiaxing/IdeaProjects/scala-train/src/main/resources/hive-site.xml"))
// println(xm2)

(xml \\ "property" \\ "name").map(_.text).foreach(println)

}
-------------End Of This ArticleThank You For Reading-------------