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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:25:37+00:00 2026-05-25T06:25:37+00:00

XML parsing in Scala doesn’t seem to be as easy and straightforward as it

  • 0

XML parsing in Scala doesn’t seem to be as easy and straightforward as it should be.

What I needed was something that behaved similar to document.getElementsByTagName(name) in JavaScript, but for my purposes all I needed was the first element of a particular tag-name. Here is what I ended up with:

import scala.xml.{Document, Elem, Node}
import scala.xml.parsing.ConstructingParser
def _getFirstMatchingElementByName(search: String, n: Node): Option[Node] = {
    if (n.label == search) {
        Some(n)
    } else {
        var i = 0
        var result: Option[Node] = None
        try {
            while (result == None) {
                result = _getFirstMatchingElementByName(search, n.child(i))
                i += 1
            }
        } catch {
            case e: IndexOutOfBoundsException => None
        }
        result
    }
}

It basically recurses through until a match is found or all possibilities are exhausted.

Now that the feature which required that I have this ability has been released I have reviewed this a little more and it really bugs me. I’m sure there are many Java libraries available to help parse XML, but given the native support that Scala has for generating XML (i.e. it can pretty much just be inlined anywhere), I am curious if I am missing something.

Is there a better way to do this in Scala?

  • 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-25T06:25:38+00:00Added an answer on May 25, 2026 at 6:25 am

    You doing it wrong!
    all I needed was the first element of a particular tag-name
    given this xml:

    val page = 
      <root>
        <need>text1</need>
        <doesnotneed>text2</doesnotneed>
        <doesnotneed>text3</doesnotneed>
        <need>text4</need>
      </root>
    

    Now calling this code will give you list of all nodes with given tag name:

    scala> page \\ "need"
    res3: scala.xml.NodeSeq = NodeSeq(<need>text1</need>, <need>text4</need>)
    

    To get only first one:

    scala> page \\ "need" head
    res4: scala.xml.Node = <need>text1</need>
    

    P.S. deep-first element would be treated as head.

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

Sidebar

Related Questions

I am about to write junit tests for a XML parsing Java class that
Now that I know how to parse xml in scala as a stream I
I am doing a lot of XML parsing/transformation using the org.w3c.dom classes. A Document
I have a small xml-parsing python snippet that works with ElementTree, but not with
I have a function that does xml parsing. I want to make the function
I'm writing a small Scala app that does the following: 1) Read XML/XHTML files
I have a single xml parsing function that I'm trying to call multiple times
I am parsing XML file in scala using val data = XML.loadFile(changes.xml) I have
My friend has an XML parsing example that I tried following, but it didn't
I'm parsing an xml file, that has nodes with text like this: <img src=someUrl1>

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.