2

so i have this XMl

<a>blah</a>

And i want to change it to

<a>someValueIDoNotKnowAtCompileTime</a>

Currently, I am looking at this SO question . However, this just changes the value to "2"

What i want is exactly the same thing, but to be able to define the value (so that it can change at runtime - i am reading the values from a file!)

I tried passing the value into the overridden methods, but that didn't work - compile errors everywhere (obviously)

How can i change static xml with dynamic values?

ADDING CODE

var splitString = someString.split("/t") //where someString is a line from a file
val action = splitString(0)
val ref = splitString(1)
xmlMap.get(action) match { //maps the  "action" string to some XML
    case Some(entry) => {
        val xmlToSend = insertRefIntoXml(ref,entry) 
        //for the different XML, i want to put the string "ref" in an appropriate place
    }
    ...
Community
  • 1
  • 1
bharal
  • 15,461
  • 36
  • 117
  • 195
  • I take it you're rewriting some XML you read earlier, and not generating it from the start? Can you post the code you've got so far? – mikołak Jul 17 '13 at 17:58
  • Some good answers here. Also see http://stackoverflow.com/a/23092226/35274 – Philippe Aug 04 '16 at 21:07

1 Answers1

3

For example:

scala> val x = <foo>Hi</foo>
x: scala.xml.Elem = <foo>Hi</foo>

scala> x match { case <foo>{what}</foo> => <foo>{System.nanoTime}</foo> }
res1: scala.xml.Elem = <foo>213370280150006</foo>

update with linked example:

import scala.xml._
import System.{ nanoTime => now }

object Test extends App {
  val InputXml : Node =
    <root>
      <subnode> <version>1</version> </subnode>
      <contents> <version>1</version> </contents>
    </root>
  def substitution = now   // whatever you like
  def updateVersion(node: Node): Node = node match {
    case <root>{ ch @ _* }</root> => <root>{ ch.map(updateVersion )}</root>
    case <subnode>{ ch @ _* }</subnode> => <subnode>{ ch.map(updateVersion ) }</subnode>
    case <version>{ contents }</version> => <version>{ substitution }</version>
    case other @ _ => other
  }
  val res = updateVersion(InputXml)
  val pp = new PrettyPrinter(width = 2, step = 1)
  Console println (pp format res)
}
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Yes, for real. The answer i linked to had two transformers and all sorts of method defined. This makes it seem fairly straightforward. – bharal Jul 17 '13 at 23:17
  • @bharal Sorry, it seems I don't know what you're asking. Oh wait, it seems you didn't link to Dan Sobral's answer http://stackoverflow.com/a/1306415/1296806 I'll take another look when I get a sec. – som-snytt Jul 17 '13 at 23:45
  • 1
    For the record, SO needs a way I can ask for "Daniel Sobral's best answer", because in this case he gives a couple of answers, each time saying, "I learned something new and now this is really the right answer." That's OK, but SO should sort it out for the archaeologist. – som-snytt Jul 17 '13 at 23:49
  • 1
    Maybe @daniel-c-sobral knows of a mechanism such as I asked for a year ago. BTW, Jul 17 is the birthday of my daughter and my wife; kind of surprised I had time for SO that day. – som-snytt Aug 30 '14 at 22:44
  • i am glad you posted on that day, and although i only just around to marking this as "answered", i do recall being *very* glad for your answer back then. Your wife and daughter are blessed with a very helpful husband/father. – bharal Aug 30 '14 at 22:52
  • @som-snytt could you give an explanation of what the @ _ * does exactly? – grinch Sep 04 '14 at 02:28
  • @grinch `v @ Pat` just binds a name to what was matched; `_*` is sequence wildcard to capture rest of Seq that was matched. Non-xml example at http://www.scala-lang.org/files/archive/nightly/2.11.x/api/2.11.x/#scala.util.matching.Regex – som-snytt Sep 04 '14 at 05:40