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))
}

Comments are closed.