读取文件和网络数据
一行一行读
一个字母一个字母读
通过网络读
读取mysql
读取xml文件
读取XML属性内容
import scala.xml.XML
object XMLApp {
def main(args: Array[String]): Unit = {
// loadXML()
readXMLAttr()
}
def readXMLAttr() = {
val xml = XML.load(this.getClass.getClassLoader.getResource("xxj.xml"))
//打印一下结构
println(xml)
//header/field header下的field
val headerField = xml \ "header" \ "field"
println(headerField)
//all field所有的field
val fields = xml \\ "field"
for (field <- fields) {
println(field)
}
//header/field/name header下field下name的值
val fieldAttributes = (xml \ "header" \ "field").map(_ \ "@name")
val fieldAttributes = (xml \ "header" \ "field" \\ "@name")
for (fieldAttribute <- fieldAttributes) {
println(fieldAttribute)
}
//name=hahahaha的message标签
val filters = (xml \\ "message")
.filter(_.attribute("name")
.exists(_.text.equals("hahahaha")))
val filters = (xml \\ "message")
.filter(x => ((x \ "@name").text)
.equals("hahahaha"))
for (filter <- filters) {
println(filter)
}
//header/field/name content获取内容
(xml \ "header" \ "field")
.map(x => (x \ "@name", x.text, x \ "@required"))
.foreach(println)
}
def loadXML(): Unit = {
val xml1 = XML.load(this.getClass.getClassLoader.getResource("test.xml"))
// println(xml1)
val xml2 = XML.load(new FileInputStream("C:\\workspaces\\workspack_scala_2\\src\\main\\resources\\test.xml"))
// println(xml2)
val xml3 = XML.load(new FileInputStream("C:\\workspaces\\workspack_scala_2\\src\\main\\resources\\test.xml"))
// println(xml3)
}
}