title: How to iterate over XML elements which can have optional attributes?
body: <p>I try to iterate over a XML file to fetch all elements of a specific type <code>&lt;Card&gt;</code>. This type can also have attributes which are optional. </p>
  
  <p>XML Sample:</p>
  
  <pre><code>&lt;Lesson&gt;
    &lt;Description&gt;Description&lt;/Description&gt;
    &lt;Card learnedTimestamp="1234567", isRepeatedByTyping="true", batch="5"&gt;
        &lt;FrontSide&gt;Foo1&lt;/FrontSide&gt;
        &lt;ReverseSide&gt;Bar1&lt;/ReverseSide&gt;
        &lt;InformationLine&gt;Info1&lt;/InformationLine&gt;
    &lt;/Card&gt;
    &lt;Card&gt;
        &lt;FrontSide&gt;Foo2&lt;/FrontSide&gt;
        &lt;ReverseSide&gt;Bar2&lt;/ReverseSide&gt;
        &lt;InformationLine&gt;Info2&lt;/InformationLine&gt;
    &lt;/Card&gt;
  &lt;/Lesson&gt;
  </code></pre>
  
  <p>Scala code:</p>
  
  <pre><code>    class XMLParser(fqFileName: String) {
        val pauDoc: Elem = XML.loadFile(fqFileName)
        def printXMLFile() = {
          var cardCount = 0
          val lesson = (pauDoc \\ "Lesson")    
          for(val card &lt;- 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)      
          }    
        }
      }
  </code></pre>
  
  <p>But this code only works if the <code>&lt;Card&gt;</code> element does not have any attribute. Does anybody know how I deal with attributes, especially if they are optional?</p>
  
  <p>P.S.: This is the exception i get.</p>
  
  <pre><code>    Exception in thread "main" org.xml.sax.SAXParseException: Element type "Card" must be followed by either attribute specifications, "&gt;" or "/&gt;".
          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.&lt;init&gt;(XMLParser.scala:7)
          at de.htwg_konstanz.ecardman.common.XMLParserMain$.main(XMLParserMain.scala:23)
          at de.htwg_konstanz.ecardman.common.XMLParserMain.main(XMLParserMain.scala)
  </code></pre>