Given the following xml, how can I lazily parse out contact records? The goal is to get the xml structure of each contact element and pass it to a function that will handle importing that record into a database.
I'll be working with a very large file and would like to avoid loading the entire xml structure in memory.
I've done this before in java using Stax, but I'm trying to figure out how to do something similar in clojure.
<?xml version="1.0"?>
<contact_list>
<contact id="1">
<first>Joe</first>
<last>Smith</last>
<email>joe@example.com</email>
</contact>
<contact id="2">
<first>Jane</first>
<last>Smith</last>
<email>jane@example.com</email>
</contact>
<contact id="3">
<first>John</first>
<last>Smith</last>
<email>John@example.com</email>
</contact>
</contact_list>
Right now I've just got the code below that returns the entire structure.
(use '[clojure.data.xml :as xml])
(use '[clojure.java.io :as io])
(defn handle-contact
"Do something with a contact"
[contact]
(println contact)
)
(defn parse-contacts
"Parse individual contact records"
[x]
(xml/parse (io/input-stream x)))
No comments:
Post a Comment