Tech behind Tech

Raw information. No finesse :)

Posts Tagged ‘xml

Parsing XML in Clojure

with 5 comments

Problem :

We need to parse a xml string and be able to query using xpath style tag list.

Ex :


I need a function that can do this,

(get-value xml :person :name)

returns “Siva”

Solution :

To parse and query xml we need to do these following three things in clojure.

1) Convert xml string (file) to Struct Map

Clojure core comes with a build in xml library (clojure.xml that has a parse function which takes in InputStream and returns a struct map that represents xml.

(defn get-struct-map [xml]
  (let [stream (ByteArrayInputStream. (.getBytes (.trim xml)))]
    (xml/parse stream)))
user> (get-struct-map xml)
{:tag :friends, :attrs nil, :content [{:tag :person, :attrs nil, :content [{:tag :name, :attrs nil, :content ["Siva"]}]}]}

This struct map is cumbersome to query.

2) Convert Struct Map to Zipper Data Structure

“A zipper is a technique of representing an aggregate data structure so that it is convenient for writing programs that traverse the structure arbitrarily and update its contents, especially in purely functional programming languages.”

To make it easy for us to traverse we will change struct map to zipper data structure. Clojure comes with zip library ( it is short form for zipper )

We will this zip library to convert xml struct map to zipper data structure.

user> ( xml-struct)
[{:tag :friends, :attrs nil, :content [{:tag :person, :attrs nil, :content [{:tag :name, :attrs nil, :content ["Siva"]}]}]} nil]

3) Use Zip-filter library and query zipper data structure.

Now that we have our xml in zipper data structure we could use zip-filter library that is present in clojure.contrib.

user> (> zipper-struct :person :name)

Putting this altogether

(ns com.sivajag.utils.xml
  (:import ( ByteArrayInputStream))
  (:require [clojure.xml :as xml])
  (:require [ :as zip])
  (:require [ :as zf]))

(defn get-struct-map [xml]
  (if-not (empty? xml)
    (let [stream (ByteArrayInputStream. (.getBytes (.trim xml)))]
      (xml/parse stream))))

(defn get-value [xml & tags]
  (apply zf/xml1-> (zip/xml-zip (get-struct-map xml)) (conj (vec tags) zf/text)))

user> (get-value xml :person :name)

Happy Coding!!!

Written by Siva Jagadeesan

June 25, 2010 at 1:57 pm

Posted in Clojure

Tagged with , , ,


Get every new post delivered to your Inbox.

Join 146 other followers

%d bloggers like this: