本文整理汇总了Scala中scala.xml.Text类的典型用法代码示例。如果您正苦于以下问题:Scala Text类的具体用法?Scala Text怎么用?Scala Text使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Text类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TrainPage
//设置package包名称以及导入依赖的类
package scrape.model
import scala.util.Try
import scala.xml.{NodeSeq, Text}
case class TrainPage(
name: String,
number: String,
code: Option[Int],
stops: List[StopDetail],
url: String,
car: List[String],
remark: List[String],
day: String
)
object TrainPage {
import utils.ParseHelper._
def fromXML(xml: NodeSeq, url: String): Either[String, TrainPage] = {
import utils.EitherUtil.eitherToRightProjection
for {
container <- (xml \\ "div" find (_ \ "@id" contains Text("container02"))).toRight("container")
table <- (container \\ "table" find (_ \ "@cellpadding" contains Text("5"))).toRight("table")
trs = table \ "tbody" \ "tr"
name <- extractValueFromTr(trs, 0)(norm).toRight("name")
number <- extractValueFromTr(trs, 1)(norm).toRight("number")
code = extractValueFromTr(trs, 2)(norm).flatMap { it => Try(it.toInt).toOption }
car <- extractValueFromTr(trs, 3)(normList(',')).toRight("car")
remark <- extractValueFromTr(trs, 4)(normList(',')).toRight("remark")
day <- extractValueFromTr(trs, 5)(norm).toRight("day")
stops = trs.view(8, trs.length).flatMap(StopDetail.fromXML).toList
} yield { TrainPage(name.replace(norm("?"), "?"), number, code, stops, url, car, remark, day) }
}
private def extractValueFromTr[A](xml: NodeSeq, index: Int)(f: String => A): Option[A] = for {
line <- xml.lift(index)
raw <- (line \ "td").lift(1)
} yield f((raw \ "span").text)
}
示例2: SelectionController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import play.api.data.Form
import play.api.data.Forms._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, Controller}
import scala.xml.{Node, Text}
class SelectionController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def index() = Action {
implicit request => val xml : UserXML = xmlForm.bindFromRequest().get
val p = scala.xml.XML.loadString(xml.xml)
val pageId = (p \ "state" \ "page-id").text
val title = (p \ "header" \ "title").text
val selections = p \ "selection"
var selection = selections
for (e <- selections){
if ((e \ "@page-id").text == pageId){
selection = e
}
}
// val selection = (p \\ "_").filter(attributeEquals("@page-id", Text(pageId)))
val header1 = (selection \ "header1").text
val header2 = (selection \ "header2").text
val options = selection \ "option"
var optionsCode : String = ""
for (e <- options){
val mapToEncode = Map("page-id"->(selection \ "next").text, (selection \ "@tag")-> (e \ "@short"))
var stringMap = ""
for ((k, v) <- mapToEncode) {
stringMap = stringMap + k + "=" + v + ":"
}
optionsCode += "<form method=\"POST\" action=\"/bind/\" style=\"text-align: center\">" +
"<input type=\"text\" name=\"xml\" value=\"" + scala.xml.Utility.escape(xml.xml) + "\" style=\"display: none\" />" +
"<input type=\"text\" name=\"map\" value=\"" + stringMap + "\" style=\"display: none\" />" +
"<input type=\"submit\" value=\"> " + (e \ "@value").text + "\" class=\"answer\" onMouseOver=\"document.getElementById('description').innerHTML = '" + e.text + "';\" onMouseOut=\"document.getElementById('description').innerHTML = '';\"/>" +
"</form>"
}
Ok(views.html.main(title)(content = views.html.selection(header1, header2, optionsCode, xml.xml)))
}
def attributeEquals(name: String, value: Text)(node: Node) = node.attributes.exists(_ == value)
val xmlForm = Form(
mapping(
"xml" -> nonEmptyText
)(UserXML.apply)(UserXML.unapply)
)
}
示例3: TextController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import play.api.data.Form
import play.api.data.Forms._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, Controller}
import scala.xml.{Node, Text}
class TextController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def index() = Action {
implicit request => val xml : UserXML = xmlForm.bindFromRequest().get
val p = scala.xml.XML.loadString(xml.xml)
val pageId = (p \ "state" \ "page-id").text
val title = (p \ "header" \ "title").text
val texts = p \ "text"
var text = texts
for (e <- texts){
if ((e \ "@page-id").text == pageId){
text = e
}
}
val header1 = (text \ "header1").text
val header2 = (text \ "header2").text
val tag = (text \ "@tag").text
val default = (text \ "@default").text
val accept = "> " + (text \ "@accept").text
val nextPage = (text \ "next").text
Ok(views.html.main(title)(content = views.html.text(header1, header2, tag, default, accept, nextPage, xml.xml)))
}
def attributeEquals(name: String, value: Text)(node: Node) = node.attributes.exists(_ == value)
val xmlForm = Form(
mapping(
"xml" -> nonEmptyText
)(UserXML.apply)(UserXML.unapply)
)
}
示例4: ExerciseEight
//设置package包名称以及导入依赖的类
package chapter.sixteen
import scala.collection.mutable.Map
import scala.xml.{ Elem, Text }
object ExerciseEight {
def dlToMap(dl: Elem): Map[String, String] = {
val map = Map[String, String]()
val children = dl.child
require(children.size % 2 == 0)
for (i <- children.indices by 2) {
(children(i), children(i + 1)) match {
case (<dt>{Text(key)}</dt>, <dd>{Text(value)}</dd>) => map(key) = value
case _ => ; // throw exception?
}
}
map
}
}
示例5: SimpleTransformationStd
//设置package包名称以及导入依赖的类
package net.michalsitko.xml.bench
import java.io.StringWriter
import scala.xml.{Elem, Node, Text, XML}
object SimpleTransformationStd extends SimpleTransformation {
val doTransform: PartialFunction[Node, Node] = {
case el: Elem if el.label == "f" =>
if(el.child.size == 1) {
val replaceWith = el.child.head match {
case t: Text =>
Text(t.text.toUpperCase)
case a => a
}
el.copy(child = List(replaceWith))
} else {
el
}
case a => a
}
override def transform(input: String): String = {
val xml = XML.loadString(input)
val transformed = xml.map {
case el: Elem if el.label == "a" =>
el.copy(child = el.child.flatMap { el =>
doTransform(el)
})
case a => a
}
val writer = new StringWriter
XML.write(writer, transformed.head, "UTF-8", true, null)
writer.toString
}
}
示例6: Util
//设置package包名称以及导入依赖的类
package trap.xml
//import java.io.BufferedInputStream
//import java.io.BufferedOutputStream
//import java.io.BufferedReader
//import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader
import scala.xml.Elem
import scala.xml.Attribute
import scala.xml.Text
import scala.xml.Node
import scala.xml.NodeSeq
import scala.xml.Null
import scala.xml.PrettyPrinter
import scala.xml.XML
import trap.Util._
import trap.file.Util._
object Util {
def formatNicely(xmlFile:String) =
writeToTextFile(xmlFile, new PrettyPrinter(200, 2).format(loadXML(xmlFile)))
def formatNicely(xml:Elem) = new PrettyPrinter(200, 2).format(xml)
def getAttributeText(xml:NodeSeq, attrName:String) = {
printdbg (" ===> getAttributeTest "+xml+": "+attrName)
(xml \ ("@"+attrName)).text
}
private val r = <root/>
def getNode(n:String) = r.copy(label=n)
def addChild(n: Node, newChild: Node) = n match {
case Elem(prefix, label, attribs, scope, child @ _*) =>
Elem(prefix, label, attribs, scope, child ++ newChild : _*)
case _ => throw new Exception("Can only add children to elements!")
}
def addChildren(n:Node, children:NodeSeq) = children.foldLeft(n)((x, y) => addChild(x, y))
def addAttribute(n:Elem, attrName:String, attrValue:String) = n % Attribute(None, attrName, Text(attrValue), Null)
def attr(xml:Node, attrName:String) = (xml \ ("@"+attrName)).text
def loadXML(fileName:String) =
using(new FileInputStream(fileName)){
fis => using(new InputStreamReader(fis)){
isr => XML.load(isr)
}
}
def getElementName (x:Node) = x.nameToString(new StringBuilder).toString
def filterByAttr(xml:NodeSeq, attrName:String, attrValue:String) =
xml.filter(x => (x \ ("@"+attrName)).text == attrValue)
def getElementsWithAttribute(xml:NodeSeq, elementName:String, attrName:String, attrVal:String) =
xml \ elementName collect { x => (x \ ("@"+attrName)).map(_.text).contains(attrVal) match {
case true => x
}
}
def getElementWithAttribute(xml:NodeSeq, elementName:String, attrName:String, attrVal:String) =
((xml \ elementName).find{ x => (x \ ("@"+attrName)).map(_.text).contains(attrVal) }).get
def getChildren(xml:NodeSeq) = xml \ "_"
}
示例7: MySnippet
//设置package包名称以及导入依赖的类
package code.snippet
import net.liftweb.http.js.JsCmd
import net.liftweb.http.js.JsCmds.SetHtml
import net.liftweb.http.SHtml
import net.liftweb.util.Helpers._
import scala.xml.Text
class MySnippet {
def render = {
var count: Int = 0
def buttonClicked(): JsCmd = {
count = count + 1
SetHtml("result", Text(s"You clicked me $count times"))
}
"button" #> SHtml.ajaxButton("Click Me", () ? buttonClicked())
}
}
示例8: Clock
//设置package包名称以及导入依赖的类
package code.comet
import net.liftweb.http.{CometActor, RenderOut}
import net.liftweb.http.js.JsCmds.SetHtml
import net.liftweb.util.Schedule
import net.liftweb.util.TimeHelpers.now
import scala.xml.{Elem, Text}
class Clock extends CometActor {
case object Tick
// schedule a Tick periodically
private val updatePeriod = 1000L
Schedule.schedule(this, Tick, updatePeriod)
def render: RenderOut =
"time" #> timeSpan
def timeSpan: Elem =
<span id="time">{now}</span>
override def lowPriority : PartialFunction[Any, Unit] = {
case Tick =>
// send a little Javascript to the browser to set the contents of
// the element with an id of 'time' to the current time.
partialUpdate(SetHtml("time", Text(now.toString)))
// schedule another update in 1 second
Schedule.schedule(this, Tick, updatePeriod)
}
}