English 中文(简体)
• 如何将具有可选择特性的十多种语文要素加以利用?
原标题:How to iterate over XML elements which can have optional attributes?

我试图通过XML文件收集特定类型的所有内容:<代码><Card>。 这种特性也可以是选择性的。

XML 样本:

<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代码:

    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.: 这是例外情况。

    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)
最佳回答

对我的工作是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()

印本:

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

难道不希望什么? 我只使用<代码>String,而不是loadFile,但这只是用于测试。

问题回答

暂无回答




相关问题
.NET DataSet.GetXml() - what s the default encoding?

Existing app passes XML to a sproc in SQLServer 2000, input parameter data type is TEXT; The XML is derived from Dataset.GetXML(). But I notice it doesn t specify an encoding. So when the user ...

Problem in XML parser program

I am doing a simple XML parser program.I am running it in command prompt.The program (abc.java) compiles successfully.But it gives following error after running it: C:Program FilesJavajdk1.6.0_13...

XML parsing problems

i need to parse this such that i can get the attribute of MMV and all the attributes of all CS tags <MMV val="Configdes000110010101"> <CS protocol="SNMP" CommandString="...

nsxmlparser not solving &apos;

Im using NSXMLParser to dissect a xml package, I m receiving &apos inside the package text. I have the following defined for the xmlParser: [xmlParser setShouldResolveExternalEntities: YES]; ...

Example of ASIHTTPRequest with TouchXML

I am currently using NSXMLParser mathods to parse my data something like this : But I found some good option as TouchXML . I googled for some good example or tutorial But I can t understand the ...

Regex to get value within tag

I have a sample set of XML returned back: <rsp stat="ok"> <site> <id>1234</id> <name>testAddress</name> <hostname>anotherName</hostname> ...

BioPython: extracting sequence IDs from a Blast output file

I have a BLAST output file in XML format. It is 22 query sequences with 50 hits reported from each sequence. And I want to extract all the 50x22 hits. This is the code I currently have, but it only ...

热门标签