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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:32:12+00:00 2026-05-15T04:32:12+00:00

Now that I know how to parse xml in scala as a stream I

  • 0

Now that I know how to parse xml in scala as a stream I need help understanding a non-trivial example.

I’d like to parse the following xml as a stream and send a message (print to console for this example) whenever I’ve parsed out a full message.

I understand that stream based parsing in scala uses case classes to handle the different elements, but I’m just getting started and I don’t quite understand how to do this.

I have this working in java using a stax parser, and I’m trying to translate that into scala.

Any help would be greatly appreciated.

<?xml version="1.0" ?>
<messages>
<message>
   <to>john.doe@gmail.com</to>
   <from>jane.doe@gmail.com</from>
   <subject>Hi Nice</subject>
   <body>Hello this is a truly nice message!</body>
</message>
<message>
   <to>joe@gmail.com</to>
   <from>jane.doe@gmail.com</from>
   <subject>Hi Nice</subject>
   <body>Hello this is a truly nice message!</body>
</message>
</messages>
  • 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-15T04:32:13+00:00Added an answer on May 15, 2026 at 4:32 am

    This is for 2.8.

    The typical way to process events is to use a match statement. In my case, i always had the need to store the parents as I process elements (to know for instance in what tag the text is located):

    import scala.xml.pull._
    import scala.io.Source
    import scala.collection.mutable.Stack
    
    val src = Source.fromString(xml)
    val er = new XMLEventReader(src)
    val stack = Stack[XMLEvent]()
    def iprintln(s:String) = println((" " * stack.size) + s.trim)
    while (er.hasNext) {
      er.next match {
        case x @ EvElemStart(_, label, _, _) =>
          stack push x
          iprintln("got <" + label + " ...>")
        case EvElemEnd(_, label) => 
          iprintln("got </" + label + ">")
          stack pop;
        case EvText(text) => 
          iprintln(text) 
        case EvEntityRef(entity) => 
          iprintln(entity) 
        case _ => // ignore everything else
      }
    }
    

    Because entity are events, you will probably need to convert to text and combine them with the surrounding text.

    In the example above I only used label, but you can also use EvElemStart(pre, label, attrs, scope) to extract more stuff and you can add an if guard to match for complex conditions.

    Also if you’re using 2.7.x, I don’t know if http://lampsvn.epfl.ch/trac/scala/ticket/2583 was back-ported so, you may have issues to process text with entities.

    More to the point, just dealing with from and to for brevity (though I would not call that the Scala way):

    class Message() {
      var to:String = _
      var from:String = _
      override def toString(): String = 
        "from %s to %s".format(from, to)
    }
    
    var message:Message = _
    var sb:StringBuilder = _
    
    while (er.hasNext) {
      er.next match {
        case x @ EvElemStart(_, "message", _, _) =>
          message = new Message
        case x @ EvElemStart(_, label, _, _) if
            List("to", "from") contains label =>
          sb = new StringBuilder 
        case EvElemEnd(_, "to") => 
          message.to = sb.toString
        case EvElemEnd(_, "from") => 
          message.from = sb.toString
          sb = new StringBuilder 
        case EvElemEnd(_, "message") => 
          println(message)
        case EvText(text) if sb != null => 
          sb ++= text
        case EvEntityRef(entity) => 
          sb ++= unquote(entity) // todo
        case _ => // ignore everything else
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now while I know that you can not perform inheritance like you would in
I'm using VTD-XML to parse some XML files with Xpath expressions, now following showed
Now that I know I can no longer communicate with Twitter mashups out there,
So I know now that the debug assemblies have been intentionally left out of
Now that Google Earth has been released for Android 2.1, does anyone know what,
What i know as of now that a constructor is called when an object
Other than that I don't know if I can reproduce it now that it's
Language Integrated Query. Now I know that the acronyms are. I have seen C#
I know that .NET assembly is self-descriptive because of the metadata. Now suppose I
I know that finish() returns your activity back to your previous activity. Now I

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.