title: Processing optional xml attributes in Scala
body: <p>I have code that reads an XML file. Some of the attributes of elements that I need to process are optional. I am trying to use Option[T] to manage them. I have written the following to pimp the NodeSeq type returned by the <code>\</code> Node operator:</p>
  
  <pre><code>class NodeSeqWrapper(nodeSeq: NodeSeq) {
    def textOption: Option[String] = {
      val text = nodeSeq.text
      if (text == null || text.length == 0) None else Some(text)
    }
  }
  implicit def nodeSeqWrapper(nodeSeq: NodeSeq): NodeSeqWrapper =
    new NodeSeqWrapper(nodeSeq)
  </code></pre>
  
  <p>and then call it like this:</p>
  
  <pre><code>(node \ "@attr").textOption.getOrElse("Some default value")
  </code></pre>
  
  <p>If the node has the "attr" attribute, this code gets it value. If it does not, the value "Some default value" is returned.</p>
  
  <p>How can I improve this? Is there some way to fold the class definition into the implicit method? Is there a better way of getting "optional" attribute values? Am I using <code>Option[T]</code> "correctly"?</p>