XML file values to strings

Viewing 1 reply thread
  • Author
    Posts
    • #6594
      DataFlair TeamDataFlair Team
      Spectator

      In all programs under SOA, the fields fetched from the XML file are converted to strings before performing other transformations on them. Rest of the transformations don’t work if the conversion to string is not done first. Does this have to do with the output format of ‘loadString’ or ‘attribute’ methods in XML? What are the output formats of these methods?

      As an example, consider the below code snippet from ‘ScoreAnalysis’ program.

      .map {line => {
      val xml = XML.loadString(line)
      (0, Integer.parseInt(xml.attribute(“Score”).getOrElse(0).toString()), line)
      }

      As ‘Score’ inside the XML file is supposedly an integer, I tried using it directly in my code without converting it to String and then to Integer using ‘parseInt’. But it was throwing error at the subsequent steps.

    • #6595
      DataFlair TeamDataFlair Team
      Spectator

      In this example xml.attribute(“Score”) returns: Option[Seq[Node]]. from this option we collect the data using getOrElse(0), which return either the data value or default value (0 in this case). Now the node type of data needs to be converted into string before parsing into Integer.

      direct conversion to int will not work.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.