`
sillycat
  • 浏览: 2539460 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Scala XML Reader and Loop Elements

 
阅读更多
Scala XML Reader and Loop Elements

I read my colleague’s codes, he plan to parse the XML and convert that XML string into objects.
But the XML format can be different. So we need to loop all the elements and find the mapping and map the text value into our objects.

Here is how I loop all the label and text in XML.
package com.j2c.utils

/**
  * Created by carl on 4/27/16.
  */
object XMLReader extends App{

  println("reading XML--------------------")

  val xml =
    """
      |<book>
      |<subject>fly in the air</subject>
      |<author>Carl Luo</author>
      |<price>1343</price>
      |</book>
    """.stripMargin

  val node = scala.xml.XML.loadString(xml)
  node.child.filter(!_.isAtom).foreach{ child =>
    println("text=" + child.text)
    println("label=" + child.label)
    println("------------------------")
  }

}

Here is the result:
com.intellij.rt.execution.application.AppMain com.j2c.utils.XMLReader
reading XML--------------------
text=fly in the air
label=subject
------------------------
text=Carl Luo
label=author
------------------------
text=1343
label=price
------------------------

Process finished with exit code 0

References:
http://stackoverflow.com/questions/31843334/xml-parse-and-return-list-of-objects

http://stackoverflow.com/questions/4748098/how-to-loop-over-a-list-of-children-found-inside-a-single-scala-xml-node
分享到:
评论

相关推荐

    Scala xml解析全解教程

    scala xml parse Scala xml解析全解教程 学习用scala处理解析xml

    scala-xml_2.11-1.0.1-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.1.pom; 包含翻译后的API...

    scala-xml_2.12-1.0.6-API文档-中文版.zip

    赠送jar包:scala-xml_2.12-1.0.6.jar; 赠送原API文档:scala-xml_2.12-1.0.6-javadoc.jar; 赠送源代码:scala-xml_2.12-1.0.6-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.12-1.0.6.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.5-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.5.jar; 赠送原API文档:scala-xml_2.11-1.0.5-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.5-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.5.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.1-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.1.jar; 赠送原API文档:scala-xml_2.11-1.0.1-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.1-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.1.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.5-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.11-1.0.5.jar; 赠送原API文档:scala-xml_2.11-1.0.5-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.5-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.5.pom; 包含翻译后的API...

    scala-xml:标准的Scala XML库

    Scala-XML是Scala编程语言中的一个标准库,用于处理XML数据。这个库为开发者提供了方便、类型安全的方式来创建、解析和操作XML文档。在Scala中,XML被当作一种第一类的值,这意味着它可以像其他数据类型一样进行操作...

    scalesXml:备用Scala XML库

    与普通的Scala XML相比,它还提供了更多的类似于XPath的体验,路径看起来像XPath,并且也像它们一样工作(具有许多相同的功能和轴)。 还可以简单地消除Scala XML的许多难点,是否要更改属性? 只需在Element上执行...

    scala-xml_2.12-1.0.6-API文档-中英对照版.zip

    赠送jar包:scala-xml_2.12-1.0.6.jar; 赠送原API文档:scala-xml_2.12-1.0.6-javadoc.jar; 赠送源代码:scala-xml_2.12-1.0.6-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.12-1.0.6.pom; 包含翻译后的API...

    scala-xml_2.11-1.0.4-API文档-中文版.zip

    赠送jar包:scala-xml_2.11-1.0.4.jar; 赠送原API文档:scala-xml_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-xml_2.11-1.0.4-sources.jar; 赠送Maven依赖信息文件:scala-xml_2.11-1.0.4.pom; 包含翻译后的API...

    pine, 面向 Scala 平台的功能HTML5和XML库.zip

    pine, 面向 Scala 平台的功能HTML5和XML库 Pine是面向 Scala 平台的功能HTML5和XML库。 它支持解析。操作和呈现 HTML 。 Pine为HTML5提供的安全绑定从 MDN 。 它实现了一个可以直接从编译时或者运行时 html/xml内容...

    Scala and Spark for Big Data Analytics.pdf

    Spark itself is written with Scala and naturally, as a starting point, we will discuss a brief introduction to Scala, such as the basic aspects of its history, purposes, and how to install Scala on ...

    Scala and Spark for Big Data Analytics

    Scala and Spark for Big Data Analytics by Md. Rezaul Karim English | 25 July 2017 | ISBN: 1785280848 | ASIN: B072J4L8FQ | 898 Pages | AZW3 | 20.56 MB Harness the power of Scala to program Spark and ...

    xtract:一个易于在scala中将XML反序列化为用户类型的库

    **Xtract:Scala中的XML反序列化利器** 在软件开发中,XML作为一种常见的数据交换格式,广泛用于系统间的数据交互。然而,XML的解析和反序列化通常涉及到复杂的操作,尤其是在强类型的语言如Scala中。这时,Xtract...

    scala pom.xml

    idea 中 构建mvaen项目,pom.xml jar的导入spark功能

    Reactive.Programming.with.Scala.and.Akka.1783984341.epub

    Harness reactive programming to build scalable and fault-tolerant distributed systems using Scala and Akka About This Book Use the concepts of reactive programming to build distributed systems ...

    scala sdk scala-2.12.3

    3. **Scala REPL**:Read-Eval-Print Loop,交互式解释器,允许开发者即时测试代码片段,快速探索语言特性和功能。 4. **文档生成工具**:可以自动生成API文档,方便团队协作和维护。 5. **构建工具接口**:如 sbt...

    2018 Scala for Java Developers: A Practical Primer

    Learn Scala is split into four parts: a tour of Scala, a comparison between Java and Scala, Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala in...

    scala-csv:用于 Scala 的 CSV ReaderWriter

    scala &gt; val reader = CSVReader .open( new File &#40; " sample.csv " &#41;) 读取所有行 scala &gt; val reader = CSVReader .open( new File &#40; " sample.csv " &#41;) reader : ...

Global site tag (gtag.js) - Google Analytics