XMLhandleTest

import scala.collection.mutable.ArrayBuffer
import scala.xml._
import scala.xml.parsing.XhtmlParser
import scala.io.Source
import scala.xml.transform.{RewriteRule, RuleTransformer}

object XMLhandleTest extends App {
  //  val xml = 
  //  println(xml(0))
  //  println(xml(0)(0))
  println("-------------------------------------")
//  val xml = 
    //
  • // {Text("Opening bracket: [")} //
  • //
  • // {Text("Closing bracket: ]")} //
  • //
  • // {Text("Opening brace: {{")} //
  • //
  • // {Text("Closing brace: }}")} //
  • //
// // println(xml.isInstanceOf[Elem]) println("-------------------------------------") def getTextFromElem(elem: Elem): String = { elem match { case Elem(_, _, _, _, Text(t)) => t } } def getAtomFromElem(elem: Elem): String = { elem match { case Elem(_, _, _, _, t:Atom[_]) => t.toString() } } // println(getTextFromElem(
  • Fred
  • ))
    // println(getTextFromElem(
  • {"Fred"}
  • )) //matcherror
    // println(getAtomFromElem(
  • {"Fred"}
  • ))
    println("-------------------------------------") def getImgWithoutAlt(doc: Document): NodeSeq = doc \\ "img" filter { _ \ "@alt" isEmpty } val xhtml = """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Xhtml Valid Page

    diaporama

    """ // val parser = new XhtmlParser(Source.fromString(xhtml)) // val doc = parser.initialize.document() // // val img = getImgWithoutAlt(doc) // println(img.getClass)//class scala.xml.NodeSeq$$anon$1 // println(img)// // println(img.size) //1 // for (e <- img) println(e)// // assert(img(0) == ) println("-------------------------------------") val xhtml2 = """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Xhtml Valid Page

    diaporama

    """ // val parser = new XhtmlParser(Source.fromString(xhtml2)) // val doc = parser.initialize.document() // // (doc \\ "img").foreach{ // n => { // println(n \ "@name") // println(n \ "@src") // } // } println("-------------------------------------") val xhtml3 = """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Xhtml Valid Page

    Le site des A

    Le site des B

    """ // val parser = new XhtmlParser(Source.fromString(xhtml3)) // val doc = parser.initialize.document() // var tr = // // (doc \\ "a").foreach{ // n => { // tr = tr.copy(child = tr.child ++ {n}) // println(n \\ "@href") // } // } // // println({tr}
    )
    println("-------------------------------------") def mapToDl(m:Map[String,String]): Elem = { <dl>{ for((k,v) <- m) yield <dt>{k}dt><dd>{v}dd>}dl> } println(mapToDl(Map("A" -> "1", "B" -> "2"))) println("-------------------------------------") def dlToMap(dl: Elem): Map[String,String] = { val keys, values = ArrayBuffer[String]() dl.child.foreach{ n => n match { case <dt>{Text(t)}dt> => keys.append(t) case <dd>{Text(t)}dd> => values.append(t) } } keys.zip(values).toMap } println(dlToMap(<dl><dt>Adt><dd>1dd><dt>Bdt><dd>2dd>dl>)) println("-------------------------------------") val xhtml4 = """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Xhtml Valid Page

    imageB

    """ // val rule = new RewriteRule { // override def transform(n: Node) = n match { // case img @ if img \ "@alt" isEmpty => // img.asInstanceOf[Elem] % Attribute(null, "alt", "TODO", Null) // case _ => n // } // } // // val parser = new XhtmlParser(Source.fromString(xhtml)) // val doc = parser.initialize.document() // // val transformed = new RuleTransformer(rule).transform(doc) // // println(transformed) println("----------------------------------------------") val xhtml5 = """ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Xhtml Valid Page

    ]]>

    """ def imgTodo(doc: Document): Document = { val s = new RuleTransformer( new RewriteRule { override def transform(n: Node) = n match { case img @ <img/> if img \ "@alt" isEmpty => img.asInstanceOf[Elem] % Attribute(null, "alt", "TODO", Null) case _ => n } } ).transform(doc)(0) val transformed = new Document() transformed.dtd = doc.dtd transformed.docElem = s transformed } val parser = new XhtmlParser(Source.fromString(xhtml5)) val doc = imgTodo(parser.initialize.document()) println(doc) }

    你可能感兴趣的:(scala)