Search code examples
xmlscalapojotransient

Scala Xml auto transient when value is null


I know to generate xml with pojo I can do something like this,

    class Person(name : String, age : Int){
        def toXml() = <person><name>{ name }</name><age>{ age }</age></person>;
    }

The problem is that if name = null, the I would have

    <person><name></name><age>8</age></person>

when really, I want the node to be transient when the value is null

    <person><age>8</age></person>

Is there a clean way to accomplish this? thanks.


Solution

  • The following code works for me.

    <person>{if(name != "")  <name>{name}</name>}</person>
    

    Cheers