美文网首页
09.scala操作外部数据

09.scala操作外部数据

作者: shone_shawn | 来源:发表于2019-08-24 19:48 被阅读0次

    读取文件和网络数据

    一行一行读

    image.png

    一个字母一个字母读

    image.png

    通过网络读

    image.png

    读取mysql

    image.png image.png image.png

    读取xml文件

    image.png image.png image.png

    读取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)
      }
    
    }
    
    image.png

    相关文章

      网友评论

          本文标题:09.scala操作外部数据

          本文链接:https://www.haomeiwen.com/subject/futtectx.html