0

I am tring to write a list of Node (xml's node) to xml file.

By writing:

 val xml = <x>{nodes}</x>

where nodes are the list of nodes- I am getting all the nodes in one line .

What can I do to print every node in a new line?

1 Answer 1

2

Here's the answer. In short, you can scala.xml.PrettyPrinter for your purposes. You should also remember (you probably do) that these classes are shipped in a separate library as of 2.11 release.

E.g.:

scala> val printer = new scala.xml.PrettyPrinter(80, 2)

scala> val nodes = List(
           <lol>node level 1</lol>, 
           <bar><foo>node level 2</foo></bar>
       )

scala> printer.formatNodes(nodes)
res1: String =
<lol>node level 1</lol><bar>
  <foo>node level 2</foo>
</bar>

Actually, when I look at the result, I see that it differs somewhat from the desired output. I'd recommend using a little bit longer, but more matching to your expectations, variant:

nodes.map(node => printer.format(node)).mkString("\n")

That will separate each node by \n:

<lol>node level 1</lol>
<bar>
  <foo>node level 2</foo>
</bar>
Sign up to request clarification or add additional context in comments.

6 Comments

the command "res0.format Nodes(nodes)" doesn't work - it asks for more parameters - do you know why?
What library version do you have? Can you share the exact version of the code that doesn't work for you?
"scala-xml" % "1.0.2"
Hmm, that's strage. Maybe try to refresh your project or make sure you've got binary compatible builds of the libraries. Also, note that I updated the answer to match your question more precisely and the new variant doesn't use formatNodes. Anyway, you should check your libraries' compatibility.
in format or in formatNode it asks to send not just the node (or nodes) but a Binder as well...
|

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.