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 8001451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:05:45+00:00 2026-06-04T16:05:45+00:00

I am trying to parse the artist.getInfo call from last.fm using Scala. The Xml

  • 0

I am trying to parse the artist.getInfo call from last.fm using Scala. The Xml API of scala is really great, however I have a question of design (and maybe personal flavour).

Currently, I am parsing the data out of the XML in a companion object and hand them to the constructor of the class like this:

class ArtistProfile (
                      name: String,
                      musicBrainzId: String,
                      url: String,
                      images: List[Image],
                      streamable: Boolean,
                      listeners: Int,
                      plays: Int,
                      similarArtists:List[SimpleArtist]) 

object ArtistProfile {

def apply(xml: NodeSeq) : ArtistProfile = {
  val root = xml
  val name = SimpleArtist.extractName(root);
  val url = SimpleArtist.extractUrl(root);

  val musicBrainzId = root \ "musicBrainzId" text
  val images:List[Image] = Image.findAllIn(root)
  val streamable = (root \ "streamable" text) eq ("1")

  val listeners = (root \ "stats" \ "listeners").text.toInt
  val plays = (root \ "stats" \ "plays").text.toInt

  new ArtistProfile(
    name,
    url,
    musicBrainzId,
    images,
    streamable,
    listeners,
    plays,
    SimpleArtist.findAllIn(xml \ "similar"))
  }
}

As you can see, these are a LOT of parameters. My question is if it would be a better style (in terms of scala’s capabilities) to pass the xml NodeSeq to the ArtistProfile constructor, so the class itself takes over the parsing instead of the companion object.

I am asking this because I am trying to write my own last.fm scala library (as an introductory project to scala) and I want things to be consistent for all objects.

  1. There are a few data in the call which might not be needed by everyone. With the second approach, I could use lazy vals for object lists if they are becoming larger and normal vals otherwise. To the programmer, it won’t be a difference.
  2. On the other side, parsing of the data needed for creating the object is now seperated from the object itself (like in the example above)

Which way is the better approach to the problem?

  • 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-06-04T16:05:46+00:00Added an answer on June 4, 2026 at 4:05 pm

    I’m not sure that there’s a black-and-white answer to this. You could put auxiliary constructors in the class so that you can also create an instance from an xml node, without the need for the companion object.

    However, there are good reasons to favour the factory pattern.

    1. The class ArtistProfile can change without impacting user code, either in its fields, or to a whole new subclass
    2. It’s not a good idea to do substantial processing in constructors because the JVM doesn’t optimize it
    3. You can cache values rather than returning a new instance, if, say, the same profile is requested twice
    4. Constructors don’t make much sense from an OO point-of-view: how can an object construct itself?
    5. It follows the convention of Scala collections and case classes, and is more concise to call

    The main disadvantage of the factory pattern (assuming you make the primary constructor private), as mentioned in Effective Java, is that the user can’t create subclasses.

    I’d put them all in the companion object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to append some JSON data from the last.fm API, I have been
I trying to parse xml from an inputstream using the sax parser. The inputstream
I'm trying to parse this feed: http://musicbrainz.org/ws/1/artist/c0b2500e-0cef-4130-869d-732b23ed9df5?type=xml&inc=url-rels I want to grab the URLs inside
I'm trying to parse a MIME-Message, using SharpMimeTools and some sample Mime Messages from
I have an xml feed at this url Now im trying parse the content,
I'm trying to parse some Android XML using XmlSlurper . For a given child
I am trying to parse a xml file using dom parser. The xml fles
I am trying to read a xml file from the web and parse it
Im trying to parse a Xml file from a URL, the xml is actually
I trying to parse some xml in Google Refine using Jython and ElementTree but

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.