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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:52:21+00:00 2026-05-19T14:52:21+00:00

Expanding on a question I asked earlier about how to iterate over a collection

  • 0

Expanding on a question I asked earlier about how to iterate over a collection of nodes in scala.xml.Node found here

I wanted to take this 1 step further and ask how I could look up to a previous child inside a recursive function to get a value once I hit a specific situation

For example (the markup is here)

<html>
    <head class="foo">
        <title>Welcome</title>
    </head>
    <body>
        <div>
            <p>Foo</p>
        </div>
    </body>
</html>

With my current implementation (thanks to @knut-arne-vedaa)

def processNode(node: Node) {
  if (node.isInstanceOf[Text]) {
      if (node.text.contains("Welcome"))
      {
        //then inside here I want to go up to the prev element (head) and pull the class 
      }
    }
  node.child foreach processNode
}

I want to add another conditional to get the text “foo” from inside the class section

Any idea what I could add inside this if statement to pull this value directly? Also how can I return the String value from this fx? a break w/ a simple return line or ?

  • 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-19T14:52:22+00:00Added an answer on May 19, 2026 at 2:52 pm

    Note that the parent of the Text node in your example is actually the <title> node.

    Since Nodes don’t have references to their parents, one way to access them is just to pass them with you when walking down the tree, like this:

    def processNode(node: Node, parent: Option[Node]) {
      if (node.isInstanceOf[Text]) {
          if (node.text.contains("Welcome"))
          {
            println("Node: " + node)
            println("Parent: " + parent.getOrElse("[toplevel]"))
          }
        }
      node.child foreach { n: Node => processNode(n, Some(node)) }
    }
    processNode(xml, None)
    

    Of course, this becomes unwieldy when you want to climb back up the tree to an arbitrary level. One approach to this is to wrap your nodes in a SuperNode that has an optional parent reference.

    case class SuperNode(current: Node, parent: Option[SuperNode] = None)
    

    For convenience, make an implicit function to convert nodes to SuperNodes in the default case of no parent.

    implicit def nodeMakeSuper(n: Node) = SuperNode(n)
    

    Now you can navigate up the tree an arbitrary number of times, like this:

    def processNode(node: SuperNode) {
      node.current match {
          case n @ Text(t) if t.contains("Welcome") => {
              println("Node: " + n)
              node.parent match {
                  case Some(p) => {
                      println("Parent: " + p.current)
                      p.parent match {
                          case Some(gp) => println("GrandParent: " + gp.current)
                          case None => println("No grandparent!")
                      }
                  }
                  case None => println("No parent!")
              }
    
          }
          case _ => // these aren't the droids you're looking for
      }
      node.current.child foreach { child: Node => processNode(SuperNode(child, Some(node))) }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

extending the question I had asked earlier which can be found here, plotting multiple
This question was asked already here , but rather than answering the specific question,
Background Expanding upon this question. I have a collection of points (in a three
I decided to open a new question about this matter, maybe expanding this question,
This is a question I ran into about expanding on an element's JavaScript onchange
I asked a question here: Extending javascript literal object which was solved because I
I was recently asked a question, apparently in an Interview, about extending the java.lang.RuntimeException.
I recently asked this question: Expose XML or Objects - thanks all for the
I asked a few questions about SQLite here, and maybe its because I'm not
Expanding on my previous question here , I want to know if is possible

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.