Join strings with XML node between in scala

I have a list of strings and I need to join them together with <br/> tags in between. So starting from:

val list = List("line1", "line2", "line3")

I need to end up with a NodeSeq of:


It's possible the list contains only one element, in which case I should end up with a NodeSeq just of Text("line1").

Is there a one-liner to do this, using one of the higher order functions on list? I've tried to play around with foldLeft but can't seem to get it to do what I want.

-------------Problems Reply------------ ++ <br /> ++ _)

Note that we have to widen the type to scala.xml.NodeSeq manually as Text is too restrictive for the reduce method. The more concise ++ <br /> ++ _)

won’t compile.

If you don't mind using Scalaz, there's intersperse:

import scalaz._
import Scalaz._ xml.Node).intersperse(<br/>): xml.NodeSeq

Agree with what Debilski has answered. Just another way of achieving the same is,

import scala.xml._

XML.loadString("<root>" + list.mkString("<br/>") + "</root>").child:NodeSeq

But using map/reduce is a much cleaner approach.

Category:xml Views:0 Time:2011-12-19
Tags: xml scala

Related post

  • Regular expression to find instances of strings within XML nodes 2009-10-15

    I need to find all instances of strings within an xml node. To be more specific, I'd like to parse some XAML and place all strings within certain controls (label for one) and set them as attributes instead. So, instead of this <Label>My string

  • Converting string to xml nodes 2011-05-19

    i have the string, it contains xml nodes, returned from the PHP file. It's like <a>1</a><b>0</b><c>4</c>.............. Now i need to find out what value each node have i.e a, b, c...... while loading this string to

  • XSLT: Parse string as XML Node Set (concret: transform HTML-String to a node-set)? 2012-01-03

    I am facing the problem that I have in XML a node with a string, representing HTML. I need to cut this string, but, of course, this can result in an invalide HTML-Markup (e.g. if I cut the string always after 30 characters I can easily lose closing e

  • Convert data string in XML node to an InlineShape 2014-09-22

    This is cross posted: I was advised to post here by a VB developer in the cross post referenced above. I work with Word VBA and content controls quite a

  • Serialize a list and use each string as xml Node 2010-07-16

    I've ran into a problem and wondered if there's simple way of solving it. Here I have a XML template, defining some properties and their values. <Properties> <Property name="ID">10000</Property> <Property name="Name"> <SubP

  • Scala how to turn "urls" in Strings into an Anchor tag XML node. 2011-07-29

    Please first look at this example below to understand what I am talking about. The input is a string. XML(aka NodeSeq) will be the output. input string example = "Hi this is an example url" output of string example = <div

  • How to loop over a list of children found inside a single scala.xml.Node 2011-01-20

    I'm pulling down some markup from a url and returning a single scala.xml.Node like so ... def doGoogleSearch(query:String) : scala.xml.Node = { val tmpUrl = "" val tmp = tmpUrl.concat(query) val url = new URL(tmp) val conn = u

  • How to traverse up a single child node using scala.xml.Node 2011-01-20

    Expanding on a question I asked earlier about how to iterate over a collection of nodes in scala.xml.Node found here I wanted to take this 1 step further and ask how I could look up to a previous child inside a recursive function to get a value once

  • Using scala.xml.Node in scala play templates 2011-09-05

    I am using the Scala play framework. For most of my pages I keep my HTML strictly within the view templates. For one page in particular though, I would like to be able pass through some XML I have generated programatically in my controller. I have th

  • XML node vs. attribute in Scala? 2011-11-12

    Remark: please consider XPath syntax dead here, thank you. I have xml node (HTML actually), and I would like to get an attribute of it. In C# (HTMLAgilityPack) I could get attribute object by name. For example having "a" node I could ask for "href" a

  • String operations on xml nodes in Python 2011-11-14

    I am reading an xml file and want to perform string operations on the node's content. import os import elementtree.ElementTree as ET from xml.etree.ElementTree import ElementTree from xml.etree.ElementTree import tostring xml_file = os.path.abspath(_

  • How can i store attributes of an xml node in an array of strings?(Reframed) 2009-03-12

    Duplicate of how can i store the attributes of an xml node in an array of strings, you should edit the original question. I am a newbie so I am reframing earlier asked question. I have a project where my xmldoc is created with help of XmlDocument cla

  • C# Xml Node to String 2009-10-30

    I have a XML document with my data in it, multiple entries for the same node fields (StudentID = FirstName, LastName, etc). How do I convert the nodes into string values for each StudentID section? --------------Solutions------------- You don't say m

  • Scala: Given a scala.xml.Node, what's the most efficient way of getting the second (or n-th) child element? 2010-02-18

    Given a scala.xml.Node object (with white space and elements as child nodes) what's the most efficient way of getting the second (or n-th) child element? Usually I would go for the built-in (node \ "foo"), but sometimes I have to rely on the position

  • How to parse strings representing xml.dom.minidom nodes in python? 2010-05-23

    I have a collection of nodes xml.dom.Node objects created using xml.dom.minidom. I store them (individually) in a database by converting them to a string using the toxml() method of a the Node object. The problem is that I'd sometimes like to be able

  • XML Node to String in Java 2010-12-10

    I came across this piece of Java function to convert an XML node to a Java String representation: private String nodeToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer();

  • Android XML parser for simple xml node strings 2011-03-23

    I need to parse a series of simple XML nodes (String format) as they arrive from a persistent socket connection. Is a custom Android SAX parser really the best way? It seams slightly overkill to do it in this way I had naively hoped I could cast the

  • java - Geting all content of a xml node as string 2011-06-30

    I am using this code to parsing xml DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(data)); Document doc = db

  • JOIN using a 'LIKE' on an XML Node? 2011-06-30

    I've got a view that joins tables using a match on an xml node, like this example: ....CROSS APPLY xmlData.Nodes('/element/subElement').... The problem is we now have elements called 'element2' and 'element3' which don't get picked up. Is there a way

Copyright (C), All Rights Reserved.

processed in 0.150 (s). 11 q(s)