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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:41:57+00:00 2026-06-10T04:41:57+00:00

Task: HTML – Parser in Scala. Im pretty new to scala. So far: I

  • 0

Task: HTML – Parser in Scala. Im pretty new to scala.

So far: I have written a little Parser in Scala to parse a random html document.

import scala.xml.Elem
import scala.xml.Node
import scala.collection.mutable.Queue
import scala.xml.Text
import scala.xml.PrettyPrinter

object Reader {
  def loadXML = {
    val parserFactory = new org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
    val parser = parserFactory.newSAXParser()
    val source = new org.xml.sax.InputSource("http://www.randomurl.com")
    val adapter = new scala.xml.parsing.NoBindingFactoryAdapter
    val feed = adapter.loadXML(source, parser)
    feed
  }

  def proc(node: Node): String =
    node match {
      case <body>{ txt }</body> => "Partial content: " + txt
      case _ => "grmpf"
    }

  def main(args: Array[String]): Unit = {
    val content = Reader.loadXML
    Console.println(content)
    Console.println(proc(content))

  }
}
  1. The problem is that the “proc” does not work. Basically, I would like to get exactly the content of one node. Or is there another way to achieve that without matching?

  2. Does the “feed” in the loadxml-function give me back the right format for parsing or is there a better way to achieve that? Feed gives me back the root node, right?

Thanks in advance

  • 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-10T04:41:58+00:00Added an answer on June 10, 2026 at 4:41 am

    You’re right: adapter.loadXML(source, parser) gives you the root node. The problem is that that root node probably isn’t going to match the body case in in your proc method. Even if the root node were body, it still wouldn’t match unless the element contained nothing but text.

    You probably want something more like this:

    def proc(node: Node): String = (node \\ "body").text
    

    Where \\ is a selector method that’s roughly equivalent to XPath’s //—i.e., it returns all the descendants of node named body. If you know that body is a child (as opposed to a deeper descendant) of the root node, which is probably the case for HTML, you can use \ instead of \\.

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

Sidebar

Related Questions

I have a task to show digital clock (with minutes precision) on HTML page
I have the following task: there are a simple table from mysql database: <html>
I have question about XSLT1.0. The task is to write out in HTML all
TASK : I have an existing xml document (UTF-8) which uses xml namespaces and
Projects habtm Tasks On tasks/show.html.erb I have link_to Add This Task To Project, new_project_path(:task
I have an HTML dropdown list: <select name=status> <option value=close >Close Task</option> <option value=open
I have a task (for instance): create an HTML&CSS according to the given Jpg
I have a pretty complex but well-formatted HTML string with lots of links in
I have an Ant task that creates an HTML report. Is it possible to
My task is to get the HTML structure of the document without data. From:

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.