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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:28:54+00:00 2026-05-13T19:28:54+00:00

Given a scala.xml.Node object (with white space and elements as child nodes) what’s the

  • 0

Given a scala.xml.Node object (with white space and elements as child nodes) what’s the most efficient way of getting the second (or n-th) child element?

Usually I would go for the built-in (node \ "foo"), but sometimes I have to rely on the position of the element. For example, I could have two Choice groups that could be either foo or bar. The document could be

<something>
  <foo/>
  <foo/>
</something>

or

<something>
  <foo/>
  <bar/>
</something>

etc.

  • 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-13T19:28:55+00:00Added an answer on May 13, 2026 at 7:28 pm

    I like retronym’s drop(n).headOption pattern as it accounts for when you have less children than n. But I think you meant the second child node (excluding text nodes), not the second instance of the <foo> tag. With that in mind, combining with your answer or using partialMap:

    node.child.partialMap{case x:scala.xml.Elem => x}.drop(n).headOption
    
    node.child.filter(_.isInstanceOf[scala.xml.Elem]).drop(n).headOption
    

    This has to assume that you won’t want to extract text in:

    val node = <something><foo/>text</something>
    

    Efficiency wise, the only other thing I could think of is to make filter lazy if you wanted to retrieve the second child when there are a large number of children. I think this may be achieved by running filter on node.child.iterator instead.

    Edit:
    Changed toIterable to iterator.
    good point, calling drop(n) on an ArrayBuffer will cause additional allocations, also how many is hard to tell, since it seems drop is overridden in IndexSeqLike. But using the iterator would address that too. So for large number of children:

    node.child.iterator.filter(_.isInstanceOf[scala.xml.Elem]).drop(n).next
    

    If you want to have it be safe, you may need to define a function to check for hasNext.

    All of this is tested only in 2.8.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

In Scala REPL: val input = <outerTag xmlns=http://xyz> <innerTag> </innerTag> </outerTag> input\\@innerTag => <innerTag
I'm new to Scala, so I may be off base on this, I want
The scala.xml package represents XML with nodes of a labelled tree. But is this
Maven Archetypes are the templates by which you can quickly generate a running example
I have an xml schema and csv data to generate corresponding xml files. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.