本文整理汇总了Scala中scala.xml.Node类的典型用法代码示例。如果您正苦于以下问题:Scala Node类的具体用法?Scala Node怎么用?Scala Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Node类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Helpers
//设置package包名称以及导入依赖的类
import sbt._
import sbt.Keys._
import scala.xml.{Comment, Elem, Node, NodeSeq}
import scala.xml.transform.{RewriteRule, RuleTransformer}
object Helpers {
def dropIfDependency(node: Node): Node = {
new RuleTransformer(new RewriteRule {
override def transform(node: Node): NodeSeq = node match {
case e: Elem if e.label == "dependency" => NodeSeq.Empty
case _ => node
}
}).transform(node).head
}
def pickFilesFromSubpaths(
roots: Seq[String],
subpath: File,
predicate: String => Boolean): Seq[(File, String)] = {
roots
.map(file(_) / subpath.toString)
.flatMap({ f =>
Path
.allSubpaths(f)
.filter({ case (_, relativePath) => predicate(relativePath) })
.toSeq
})
}
}
示例2: Attribute
//设置package包名称以及导入依赖的类
package org.cg.scala.dhc.domelments
import org.cg.scala.dhc.util.StringUtil
import scala.xml.Node
case class Attribute(val name: String, val transformation: Option[TransformationElement], val ln: String) {
override def equals(o: scala.Any): Boolean = o.isInstanceOf[Attribute] && {
val other = o.asInstanceOf[Attribute]
name.equals(other.name) &&
transformation.equals(other.transformation) &&
ln.equals(other.ln)
}
override def toString: String = {
s"""ATTRIBUTE $name ln $ln
${StringUtil.indent(transformation.getOrElse("").toString(), Const.Indent)}
""".stripMargin
}
}
object Attribute {
def apply(a: Node): Attribute = {
val name = (a \ "name").text
val ln = (a \ "name" \ "@ln").text
val transformation: Option[TransformationElement] = TransformationElement.get(a)
Attribute(name, transformation, ln)
}
}
示例3: RawSource
//设置package包名称以及导入依赖的类
package org.cg.scala.dhc.domelments
import scala.xml.Node
case class RawSource(val rawSource: String, val rawSourceLn: String) {
override def equals(o: scala.Any): Boolean = o.isInstanceOf[RawSource] && {
val other = o.asInstanceOf[RawSource]
rawSource.equals(other.rawSource) &&
rawSourceLn.equals(other.rawSourceLn)
}
override def toString: String = s"RAW SOURCE $rawSource ln $rawSourceLn"
};
object RawSource {
def apply(t: Node): RawSource = {
val rawSource = (t \ "rawSource").text
val rawSourceLn = (t \ "rawSource" \ "@ln").text
new RawSource(rawSource, rawSourceLn)
}
}
示例4: phonebook
//设置package包名称以及导入依赖的类
package examples.phonebook
object phonebook {
val labPhoneBook =
<phonebook>
<descr>
This is the <b>phonebook</b> of the
<a href="http://acme.org">ACME</a> corporation.
</descr>
<entry>
<name>Burak</name>
<phone where="work"> +41 21 693 68 67</phone>
<phone where="mobile">+41 79 602 23 23</phone>
</entry>
</phonebook>
println( labPhoneBook )
// XML is immutable - adding an element
import scala.xml.{ Node, Text }
def add( phonebook:Node, newEntry:Node ):Node = phonebook match {
case <phonebook>{ ch @ _* }</phonebook> =>
<phonebook>{ ch }{ newEntry }</phonebook>
}
val pb2 =
add( labPhoneBook,
<entry>
<name>Kim</name>
<phone where="work"> +41 21 111 11 11</phone>
</entry> )
def main(args:Array[String]) = println( pb2 )
}
示例5: JunitResultParser
//设置package包名称以及导入依赖的类
package net.white_azalea.models.parsers.junits
import java.io.{ File, FilenameFilter }
import net.white_azalea.datas.junit.{ TestCase, TestFailure }
import net.white_azalea.models.parsers.utils.XML
import scala.xml.Node
class JunitResultParser {
def parse(xmlDir: File): List[TestCase] = {
val xmlFiles = xmlDir.listFiles(new FilenameFilter {
override def accept(dir: File, name: String): Boolean =
name.endsWith(".xml")
})
xmlFiles
.flatMap(parseSingleFile)
.toList
}
private[junits] def parseSingleFile(file: File): Seq[TestCase] = {
(XML.load(file) \ "testcase").map(readTestCase)
}
private[junits] def readTestCase(node: Node): TestCase = {
val methodName = node \@ "name"
val className = node \@ "classname"
val failures = (node \ "failure").map(readFailure)
TestCase(methodName, className, failures)
}
private[junits] def readFailure(node: Node): TestFailure = {
val message = node \@ "message"
val typeMessage = node \@ "type"
val cause = node.toString()
TestFailure(message, typeMessage, cause)
}
}
示例6: Img2TxtProcessor
//设置package包名称以及导入依赖的类
package pl.mojepanstwo.sap.toakoma.processors
import java.io.{File, FileInputStream, InputStreamReader}
import org.springframework.batch.item.ItemProcessor
import pl.mojepanstwo.sap.toakoma._
import scala.xml.{Elem, Node, XML}
import scala.xml.transform.RewriteRule
import sys.process._
class Img2TxtProcessor extends ItemProcessor[Model, Model] {
override def process(item:Model): Model = {
item.linksHtml.foreach { case (key, dirPath) =>
try {
val xml = XML.load(new InputStreamReader(new FileInputStream(item.xmlPath(key)), "UTF-8"))
val changed = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case elem @ Elem(_, "img", _, _, child @ _*) => {
val src = elem.attribute("src").get
val imageFile = new File(dirPath + "/" + src)
val cmd = "tesseract " +
imageFile.getAbsolutePath + " " +
"stdout " +
"-l pol"
var result = cmd !!
if(Option(result).exists(_.trim.isEmpty)) return n
result = result.replaceAll("-\n", "")
result = result.replaceAll("—\n", "")
return(<textFromImg>{result}</textFromImg>)
}
case elem: Elem => elem copy (child = elem.child flatMap (this transform))
case other => other
}
} transform xml
XML.save(dirPath + "/text_from_image.xml", changed(0), "UTF-8", true)
item.xmlPath(key) = dirPath + "/text_from_image.xml"
} catch {
case e: Throwable => println(e.printStackTrace())
}
}
item
}
}
示例7: 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)
)
}
示例8: 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)
)
}
示例9: RunDesignTemplateReader
//设置package包名称以及导入依赖的类
package com.pacbio.simulator
import java.nio.file.Path
import java.text.SimpleDateFormat
import java.util.{Calendar, UUID}
import com.pacbio.common.models.{XmlTemplateReader => GenTemplateReader}
import scala.xml.Node
class RunDesignTemplateReader(xmlFile: Path) {
private def randomId(): UUID = UUID.randomUUID()
private def randomContextId(): String = toRandomMovieContextId("SIM")
private var subreadSetUuid : Option[UUID] = None
def toRandomMovieContextId(instrument:String) = {
val sx = new SimpleDateFormat("yyMMdd_HHmmss").format(Calendar.getInstance().getTime)
s"m${instrument}_$sx"
}
private val getSubreadsetUuid = {
subreadSetUuid = Some(randomId())
subreadSetUuid.get
}
def read :Node =
GenTemplateReader
.fromFile(xmlFile)
.globally() .substitute("{RUN_ID}", randomId())
.perInstance() .substitute("{STATUS}", "Ready")
//.perInstance() .substitute("{movieLengthInMins}", movieLenInMins())
.perNode("SubreadSet") .substituteMap {
val collectionContextId = randomContextId()
Map(
"{SUBREAD_ID}" -> (() => getSubreadsetUuid),
"{EXTERNAL_RESOURCE_ID}" -> (() => randomId())
//"{collectionContextId}" -> (() => collectionContextId),
//"{collectionPathUri}" -> (() => s"//pbi/collections/xfer-test/$collectionContextId/1_A01/$runId/")
//"{movieLengthInMins}" -> (() => movieLengthInMins)
)
}.result()
def readStr = read.mkString
def readRundesignTemplateInfo = RunDesignTemplateInfo(readStr, subreadSetUuid.get)
}
示例10: PageDownloader
//设置package包名称以及导入依赖的类
package com.nekopiano.scala.rest.client
import dispatch.classic.url
import dispatch.classic.Http
import java.io.StringReader
import nu.validator.htmlparser.common.XmlViolationPolicy
import nu.validator.htmlparser.sax.HtmlParser
import org.xml.sax.InputSource
import scalax.io.JavaConverters._
import scalax.io.Resource
import scala.xml.Node
import scala.xml.parsing.NoBindingFactoryAdapter
object PageDownloader {
def main(args: Array[String]): Unit = {
val h = new Http
val req = url("http://www.sample.jp/products/") >\ "UTF-8"
val html = h(req as_str)
val rootNode = toNode(html)
val links = rootNode \\ "@href" filter (_.text matches "/products/[a-z]+/[a-z0-9]+/")
val pages = links.map(a => {
"http://www.sample.jp" + a.text + "shiyo.htm"
}).sorted.distinct
println("pages.size=" + pages.size)
pages.foreach(url => {
println(url)
val data = Resource.fromURL(url).byteArray
println("downloading: %s ..." format url)
val r = "[a-z]+/[a-z0-9]+/shiyo.htm".r
val productName = r.findAllIn(url).matchData.mkString.dropRight(10).replace("/", "-")
Resource.fromFile(new java.io.File("samplepages\\" + productName + ".html")).write(data)
})
}
def toNode(str: String): Node = {
val hp = new HtmlParser
hp.setNamePolicy(XmlViolationPolicy.ALLOW)
val saxer = new NoBindingFactoryAdapter
hp.setContentHandler(saxer)
hp.parse(new InputSource(new StringReader(str)))
saxer.rootElem
}
}
示例11: PageDownloader
//设置package包名称以及导入依赖的类
package com.nekopiano.scala.scalasandbox.download
import dispatch.classic.url
import dispatch.classic.Http
import java.io.StringReader
import nu.validator.htmlparser.common.XmlViolationPolicy
import nu.validator.htmlparser.sax.HtmlParser
import org.xml.sax.InputSource
import scalax.io.JavaConverters._
import scalax.io.Resource
import scala.xml.Node
import scala.xml.parsing.NoBindingFactoryAdapter
object PageDownloader {
def main(args: Array[String]): Unit = {
val h = new Http
val req = url("http://www.sample.jp/products/") >\ "UTF-8"
val html = h(req as_str)
val rootNode = toNode(html)
val links = rootNode \\ "@href" filter (_.text matches "/products/[a-z]+/[a-z0-9]+/")
val pages = links.map(a => {
"http://www.sample.jp" + a.text + "shiyo.htm"
}).sorted.distinct
println("pages.size=" + pages.size)
pages.foreach(url => {
println(url)
val data = Resource.fromURL(url).byteArray
println("downloading: %s ..." format url)
val r = "[a-z]+/[a-z0-9]+/shiyo.htm".r
val productName = r.findAllIn(url).matchData.mkString.dropRight(10).replace("/", "-")
Resource.fromFile(new java.io.File("samplepages\\" + productName + ".html")).write(data)
})
}
def toNode(str: String): Node = {
val hp = new HtmlParser
hp.setNamePolicy(XmlViolationPolicy.ALLOW)
val saxer = new NoBindingFactoryAdapter
hp.setContentHandler(saxer)
hp.parse(new InputSource(new StringReader(str)))
saxer.rootElem
}
}
示例12: ExerciseNine
//设置package包名称以及导入依赖的类
package chapter.sixteen
import scala.xml.transform.{ RuleTransformer, RewriteRule }
import scala.xml.{ Attribute, Elem, Node, Null }
object ExerciseNine {
def setDefaultImgAlt(input: Node, value: String): Seq[Node] = {
val rule = new RewriteRule {
override def transform(n: Node) = n match {
case img @ <img /> if img \ "@alt" isEmpty => img.asInstanceOf[Elem] % Attribute(null, "alt", value, Null)
case _ => n
}
}
new RuleTransformer(rule).transform(input)
}
}
示例13: ExerciseNineSpec
//设置package包名称以及导入依赖的类
package chapter.sixteen
import ExerciseNine.setDefaultImgAlt
import scala.xml.Node
import org.scalatest._
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
@RunWith(classOf[JUnitRunner])
class ExerciseNineSpec extends FlatSpec with Matchers {
"setDefaultImgAlt" should "transform an XHTML document by setting `alt` to TODO to all img elements without the attribute set" in {
val document: Node =
<html>
<img alt="TODO" />
<img />
<img alt="TODO" />
</html>
val defaultAlt = "TODO"
val transformed = setDefaultImgAlt(document, defaultAlt)
transformed should have size 1
val imgs = transformed(0) \ "img"
imgs should have size 3
imgs.foreach { img => img.attribute("alt").get.text should be (defaultAlt) }
}
}
示例14: 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
}
}
示例15: 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 \ "_"
}