XML element with optional attribute

102 visualizações
Pular para a primeira mensagem não lida

Russ P.

não lida,
24 de mar. de 2017, 02:01:3324/03/2017
para scala-user
Suppose I am printing out an XML element like this:

  def toXML: xml.Elem = {

    <waypt name={name}>
      ...
      </waypt>
    }

The "name" attribute is a String and is optional (some waypoints have names and some don't). I want to show it only if it is nonempty. In other words, I don't want to see name=""

I just want the name attribute completely omitted if the name is an empty String.

How can I do that in the toXML method above? Thanks.

Naftoli Gugenheim

não lida,
24 de mar. de 2017, 02:48:5624/03/2017
para Russ P., scala-user
I don't know if there's a better solution, but Elem is a case class so you can add the attribute on afterwards using the copy method.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Manchester

não lida,
28 de mar. de 2017, 23:49:2428/03/2017
para scala-user
Hi,

If you make "name" an Option[scala.xml.Text], I think the XML will be generated as you desire.

Dan

Russ Paielli

não lida,
29 de mar. de 2017, 01:18:3629/03/2017
para Daniel Manchester, scala-user
Nice. Thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/vqLXM7clpqo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem