From zero to learn scala (eight) annotations, XML processing

A: annotation (notes related really do not want to look back to think of it supplemented on it)

What is a comment

         // we are more familiar with this, I wrote an official explanation, write about different scala and java bar
         // annotations are inserted into the code that you have the tools so that they can deal with the label
         // java annotation is not Effect source compiled into bytecode, the bytecode they simply forget to add data,
         // so that they can use an external tool. But the scala comments may affect the compilation process

What can be annotated

          // classes, methods, fields, local variables and parameters can add annotations
          // you can add multiple notes, did not affect the order
          // main constructor add annotations, you need to comment placed before the constructor, and plus pile parentheses
          class the Inject () {}
          class Credentials @Inject () (var username: String,
          var password: String)
          // add annotations to the expression
          val myMap = Map (1-> 1,2- > 2 , 3->. 3)
          {myMap.get (. 1): @ match an unchecked {}}

          // add annotation type parameter
          class MyContainer [@specialized T]
          // add annotations actual parameter
          String @cps [Unit] // @ cps with a type parameter

Notes parameter

          

Notes realized

          

Java modifiers

          

Marker interface

          

Unchecked exceptions

          

Vararg

          

JavaBeans

          

Tail recursion

          

Generating a jump table inline

          

The method may be omitted

          

The basic type of specialization

          

For error and warning notes

          

Two: XML processing

XML literals

          //就是可以直接写XML的代码进scala代码,直接生成XML元素。即Elem类
          val doc = <html><head><title>title</title></head></html>
          //部分的XML代码也是可以的。即NodeSeq类
          val item = <li>Fred</li><li>Wilma</li>

XML字节

          val elem = <a href="http://scala-lang.org">
          The <em>Scala</em> language</a>
          println(elem.label)//a标签
          println(elem.child)//a之间的东西

          

          import scala.xml.NodeBuffer
          import scala.xml.NodeSeq

          val nodebuffer = new NodeBuffer
          nodebuffer += <li>Fred</li>
          nodebuffer += <li>Wilma</li>
          val nodes :NodeSeq = nodebuffer//成不可变的变量

Seq[Node]
NodeSeq
Iterable[Metadata] Document node
Metadata Elem SpcialNode
      Atom EntityRef ProInstr Comment
    Text PCData Unparsed    
 

元素属性

          val href = <a href="http://www.baidu.com"></a>
          println(href.attributes("href"))
          
          val image = <img src="src" alt="alt"></img>
          val alt = image.attributes("alt")
          println(alt) //alt的值没有需要转移的字符
          println(alt.text) //alt中有需要转移的字符
          
          val data = href.attributes.get("hrefa").getOrElse("")
          println(data)//获取到或者获取不到

          for(attr<- href.attributes) //遍历所有属性
                    println(attr.key + "|" + attr.value)

内嵌表达式

          //如果代码产生的是一个节点序列,序列中的节点会被直接添加到XML。
          //所有其他值会被放进Aton[T]中,这是一个针对类型T的容器(集合)
          //通过这种方法你可以在XML树种放任何值
          val change = "15"
          val href = <a href="http://www.baidu.com">change</a>
          
          //不光能放变量还能放代码,用{}进行了转义。你如果需要{}字符串可以写{{
          val data = <ul>{for(i <- 1 to 10) yield i}</ul>
          println(data)

在属性中使用表达式

          val change = Seq("http://www.baidu.com")
          val href = <a href={change.apply(0)}></a>
          //<a href="{change.apply(0)}"></a>
          //表达式用""起来这样不是代码块
          <img alt={
                    if("TODO" == "TODO") null else "aa" }></img>
          //上面的表达式中如果为null,钙元素就不会有alt属性

特殊字节类型

          //有时候你需要将非XML文本包含到XML中比如:[CDATA]
          val js = <script><![CDATA[if(1 > 0) alter(cold)]]></script>
          //上面的例子中,CDATA并不会转换成JS代码,你需要引入一个新的类
          val code ="if(1 > 0) alter(cold)"
          val js1 = <script>{PCData(code)}</script>

          //你可以在Unparsed节点中包含任意文本。他们会被原样保留大师不推荐使用,容易出错
          val n1 = <xml:unparsed><&></xml:unparsed>

类XPath表达式

          //NodeSeq类提供了类似XPath中 / 和 //操作符的方法。
          //由于//表示注释 scala使用 \ 和 \\来代替
          val list = <d1 a="a"><dt>A</dt><dt>B</dt></d1>
          val iter = list \ "dt"
          //上例子显示的是所有dt的内容,但是\只能过滤单层的元素,\\能过滤多层
          //@开头可以定位属性
          println(list \\ "d1" \ "@a")
          for(n <- list \\ "dt")
                    println(n)

模式匹配

          val list = <d1 a="a">ss</d1>
          list match {
          // case <d1/> => println("dt")
          // case <d1>{_*}</d1> => println() //直接过滤出来所有的子节点
          // case <d1>{child}</d1> => println(child)//子元素
          case <d1>{child @a}</d1> => println(child+"a")//过滤出来属性
          case _ => println("else")

修改元素和属性

          import scala.xml.Attribute
          import scala.xml.Null

          val list = <ul><li>AA</li><li>BB</li></ul>
          val list2 = list.child.++:(<li>CC</li>)//添加子元素
          println(list2.toString())

          val img = <img src= "1.jpg"></img>
          val img2 = img % Attribute(null,"alt","An image",Null)
          //第一个参数是命名空间,最后一个是元数据列表
          println(img2)

XML变换

          //如果你需要将ul节点修改成ol,你要做的就是定义一个RewriteRule
          val rule = new RewriteRule{
                    override def transform(n:Node) =
                    n match {
                              case <ul>{child}</ul> => <ol>child</ol>
                              case _ => n
                    }
          }
          val data = <data><ul><aa>aa</aa></ul></data>.child.toList.apply(0)
          val result = new RuleTransformer(rule).transform(data)
          println(result)

加载和保存

          //加载XML文件
          val root = XML.load("D://JAVA//sparkCode//Spark//bin//aa//NewFile.xml")
          val root = XML.load(new FileInputStream("D://JAVA//sparkCode//Spark//bin//aa//NewFile.xml"))
          val root = XML.load(new InputStreamReader(
          new FileInputStream("D://JAVA//sparkCode//Spark//bin//aa//NewFile.xml")))
          val root = XML.load(new URL("https://www.baidu.com/index.php"))
          println(root.toString())

          import scala.xml.parsing.ConstructingParser
          import java.io.File
          //可以保留注释的类
          val parse = ConstructingParser.fromFile(new File("D://JAVA//sparkCode//Spark//bin//aa//NewFile.xml"), preserveWS=true)
          val rootElec = parse.document().docElem

命名空间

          //XML的命名空间类似于包的概念,XML的命名空间是一个URL或者URI
          //xmlns属性也可以声明一个命名空间
          <html xmlns="http://www.baidu.com">
          </html>
          //html元素及后代都被放置在这个命名空间中,后台也可以引入自己的空间
          <avg xmlns="http://www.baidu.com">
          </avg>

Guess you like

Origin www.cnblogs.com/wuxiaolong4/p/11879254.html