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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:16:26+00:00 2026-05-28T03:16:26+00:00

I try to iterate over a XML file to fetch all elements of a

  • 0

I try to iterate over a XML file to fetch all elements of a specific type <Card>. This type can also have attributes which are optional.

XML Sample:

<Lesson>
  <Description>Description</Description>
  <Card learnedTimestamp="1234567", isRepeatedByTyping="true", batch="5">
      <FrontSide>Foo1</FrontSide>
      <ReverseSide>Bar1</ReverseSide>
      <InformationLine>Info1</InformationLine>
  </Card>
  <Card>
      <FrontSide>Foo2</FrontSide>
      <ReverseSide>Bar2</ReverseSide>
      <InformationLine>Info2</InformationLine>
  </Card>
</Lesson>

Scala code:

    class XMLParser(fqFileName: String) {
      val pauDoc: Elem = XML.loadFile(fqFileName)
      def printXMLFile() = {
        var cardCount = 0
        val lesson = (pauDoc \\ "Lesson")    
        for(val card <- lesson \\ "Card"){      
          cardCount = cardCount + 1
          println("Card No " + cardCount)
          val frontSide = (card \\ "FrontSide").text
          println("FrontSide Value: " + frontSide)      
          val reverseSide = (card \\ "ReverseSide").text
          println("ReverseSide Value.text: " + reverseSide)      
          val infoLine = (card \\ "InformationLine").text
          println("InformationLine Value: " + infoLine)      
        }    
      }
    }

But this code only works if the <Card> element does not have any attribute. Does anybody know how I deal with attributes, especially if they are optional?

P.S.: This is the exception i get.

    Exception in thread "main" org.xml.sax.SAXParseException: Element type "Card" must be followed by either attribute specifications, ">" or "/>".
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(Unknown Source)
        at scala.xml.factory.XMLLoader$class.loadXML(XMLLoader.scala:40)
        at scala.xml.XML$.loadXML(XML.scala:40)
        at scala.xml.factory.XMLLoader$class.loadFile(XMLLoader.scala:49)
        at scala.xml.XML$.loadFile(XML.scala:40)
        at de.htwg_konstanz.ecardman.common.XMLParser.<init>(XMLParser.scala:7)
        at de.htwg_konstanz.ecardman.common.XMLParserMain$.main(XMLParserMain.scala:23)
        at de.htwg_konstanz.ecardman.common.XMLParserMain.main(XMLParserMain.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-28T03:16:27+00:00Added an answer on May 28, 2026 at 3:16 am

    This works for me on 2.9.1:

    val raw = """<Lesson>
      <Description>Description</Description>
      <Card learnedTimestamp="1234567" isRepeatedByTyping="true" batch="5">
          <FrontSide>Foo1</FrontSide>
          <ReverseSide>Bar1</ReverseSide>
          <InformationLine>Info1</InformationLine>
      </Card>
      <Card>
          <FrontSide>Foo2</FrontSide>
          <ReverseSide>Bar2</ReverseSide>
          <InformationLine>Info2</InformationLine>
      </Card>
    </Lesson>"""
    
    class XMLParser(fqFileName: String) {
      import xml._
      val pauDoc: Elem = XML.loadString(fqFileName)
      def printXMLFile() = {
        var cardCount = 0
        val lesson = (pauDoc \\ "Lesson")    
        for(card <- lesson \\ "Card"){      
          cardCount = cardCount + 1
          println("Card No " + cardCount)
          val frontSide = (card \\ "FrontSide").text
          println("FrontSide Value: " + frontSide)      
          val reverseSide = (card \\ "ReverseSide").text
          println("ReverseSide Value.text: " + reverseSide)      
          val infoLine = (card \\ "InformationLine").text
          println("InformationLine Value: " + infoLine)      
        }    
      }
    }
    
    new XMLParser(raw).printXMLFile()
    

    This prints:

    Card No 1
    FrontSide Value: Foo1
    ReverseSide Value.text: Bar1
    InformationLine Value: Info1
    Card No 2
    FrontSide Value: Foo2
    ReverseSide Value.text: Bar2
    InformationLine Value: Info2
    

    Isn’t it what you expect? I just used loadString instead of loadFile, but that’s just for testing.

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

Sidebar

Related Questions

I have scala map: attrs: Map[String , String] When I try to iterate over
Let's say I have collection with 100 elements. Regular enumerator would iterate over those
I have an array: deleteIds= [User1]; and try to iterate over it as like:
I got an array which I iterate over and try to create a variable.
Try as I might, I can't get a JNLP file to run locally (via
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
I have a JavaScript function where someone can pass anything in, and I iterate
My XML file looks like this: <strings> <string>Bla <b>One &amp; Two</b> Foo</string> </strings> I
I have a zip file which can contain any number of zipfiles inside it
I have a function that reads certain tags out of an XML file. 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.