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
    }
    ...
2
  • 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? Commented Jul 17, 2013 at 17:58
  • Some good answers here. Also see stackoverflow.com/a/23092226/35274 Commented Aug 4, 2016 at 21:07

1 Answer 1

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)
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, for real. The answer i linked to had two transformers and all sorts of method defined. This makes it seem fairly straightforward.
@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 stackoverflow.com/a/1306415/1296806 I'll take another look when I get a sec.
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.