Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:18:53+00:00 2026-05-26T03:18:53+00:00

I have code that reads an XML file. Some of the attributes of elements

  • 0

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 \ Node operator:

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)

and then call it like this:

(node \ "@attr").textOption.getOrElse("Some default value")

If the node has the “attr” attribute, this code gets it value. If it does not, the value “Some default value” is returned.

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 Option[T] “correctly”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:18:54+00:00Added an answer on May 26, 2026 at 3:18 am

    I would say you are doing it in a very idiomatic way, yes.

    You can “fold the definitions” as follows:

    implicit def enrichNodeSeq(nodeSeq: NodeSeq) = new AnyRef {
      def textOption : Option[String] = {
        val text = nodeSeq.text
        if (text == null || text.length == 0) None else Some(text)
      }
    }
    

    If you are always applying .getOrElse(...) on the result, you may also want to define a second version textOrElse(elze : String) : String:

    implicit def enrichNodeSeq(nodeSeq: NodeSeq) = new AnyRef {
      def textOption : Option[String] = {
        val text = nodeSeq.text
        if (text == null || text.length == 0) None else Some(text)
      }
    
      def textOrElse(elze : String) : String = textOption.getOrElse(elze)
    }
    

    That will make things slightly more concise.

    scala> (<b>Hello</b> : NodeSeq).textOrElse("No text found.")
    resN: String = Hello
    scala> (<br /> : NodeSeq).textOrElse("No text found.")
    resM: String = No text found.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my .aspx.cs I have a code that reads a .xml file and I
We have some C# code that reads data from a text file using a
I have an Asp.net app that simply reads an xml file and this code
I have a code as such that reads in an xml file into a
So I have some perl code that goes something like: use strict; use XML::XPath;
I have some code that parses an xml file using XDocument . It retrieves
I have a bit of code that basically reads an XML document using the
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I have some code that is reading a config file, but when I open
I have a xml file that changed when i update some values, and i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.