Warning: Trying to access array offset on value of type bool in /home/www/blog/wp-content/themes/catch-box/functions.php on line 1079

Caching XML access in Scala (example)

I want to create a hashed map for fast access to specific XML tags and attributes.

I used a config xml file of JDBCluster and its attribute name of the tag property. At the end of execution ht contains the following:

Map(Car -> List(longitude, latitude), Car2 -> List(longitude2, latitude2))

Here is the code:

object XmlTest extends Application {
  val testXML =
  <jdbcluster>
    <daotype>
      <dao id="Car" class="dao.Car">
        <property name="longitude">
          <value>1.234</value>
        </property>
        <property name="latitude">
          <value>5.678</value>
        </property>
      </dao>
      <dao id="Car2" class="dao.Car2">
        <property name="longitude2">
          <value>1.234</value>
        </property>
        <property name="latitude2">
          <value>5.678</value>
        </property>
      </dao>
    </daotype>
  </jdbcluster>

  val ht = new HashMap[String, Seq[String]]

  val l = for (dao <- testXML \ "daotype" \ "dao") yield
    (dao \ "@id" text, for (p <- dao \ "property") yield
      p.attributes("name").text)

  l.foreach (x => ht.put(x._1, x._2))
}