Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:48:15+00:00 2026-06-04T01:48:15+00:00

I have a need to serialize a Clojure data structure to XML, but I’m

  • 0

I have a need to serialize a Clojure data structure to XML, but I’m stumbling on how to emit a collection. To make this more concrete, assume I have a Clojure map as follows:

    (def parking-lot {:parking-lot #{
                         {:car {:color "red", :make "nissan", :year 2003}}
                         {:car {:color "blue", :make "toyota", :year 2001}}
                         {:car {:color "black", :make "honda", :year 2010}}}})

This is just a map with one element whose value is a set of “car” items. Now, let’s assume that I want to serialize this map to produce the following XML:

    <? xml version="1.0" ?>
    <parking-lot>
      <car color="red" make="nissan" year="2003" />
      <car color="blue" make="toyota" year="2001" />
      <car color="black" make="black" year="2010" />
    </parking-lot>

Searching the web for docs on how to best parse/emit XML in Clojure led me to the clojure.data.xml library, so that’s what I’m using.

Here’s a trivial example of how to use clojure.data.xml to emit some XML:

    REPL> (use 'clojure.data.xml)

        => nil
    REPL> (emit-str (element :parking-lot {}))

        => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <parking-lot></parking-lot>"

A slightly more complicated example:

    REPL> (emit-str (element :parking-lot {}
              (element :car {:color "yellow" :make "ford" :year "2000"})
              (element :car {:color "purple" :make "chevrolet" :year "1977"})))

        => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <parking-lot>
                <car color=\"yellow\" year=\"2000\" make=\"ford\"></car>
                <car color=\"purple\" year=\"1977\" make=\"chevrolet\"></car>
            </parking-lot>"

Here you see that the 3rd argument to the ‘element’ function can indeed be variable number of calls to ‘element’. Without looking deeply at the docs for the ‘element’ function, I guess I assumed that ‘element’ would be overloaded to also take a sequence of element items as its 3rd argument. And so, without looking at the docs, I just went ahead and whipped up a small block of code that would do just that:

    REPL> (emit-str (element :parking-lot {}
              (map (fn [car] (let [carattrs (:car car)]
                       (element :car {:color (:color carattrs),
                                      :make (:make carattrs),
                                      :year (:year carattrs)}))) 
                   (:parking-lot parking-lot))))

I was hoping this would work, but alas it does not. The problem is that the 3rd argument to the ‘element’ function cannot be a sequence itself.

So, at this point I’m a bit stumped as to what to do. I’m still relatively new to Lisp and Clojure, so go easy on me if my next thought is moronic. So my thought is, would this be a use case for a macro? I.e., should I create a macro that takes as its input a set (or any coll for that matter) of cars, and outputs its items as individual s-expressions? So for example, I’m thinking in my mind of a macro that behaves like this:

    REPL> (defmacro flatten-as-elements [coll-of-cars] ...)
    REPL> (flatten-as-elements #{ {:color "blue" :model "honda" year "2000"}
                                  {:color "yellow" :model "ford" year "2011"}})
        => (element :car {:color "blue" :model "honda" year "2000"})
           (element :car {:color "yellow" :model "ford" year "2011"})

In my mind at least, the output of such a macro would fit as the 3rd argument to the ‘element’ function, and would produce my desired goal. Of course, my worry is that there is some completely obvious solution that I’m overlooking, and that using a macro here is misguided. Any help from the SO community is greatly appreciated! Thank you in advance!

-Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:48:16+00:00Added an answer on June 4, 2026 at 1:48 am

    You can use apply to splice arguments into a function call:

    (emit-str
     (apply element :parking-lot {}
            (map (fn [car] (let [carattrs (:car car)]
                             (element :car {:color (:color carattrs),
                                            :make (:make carattrs),
                                            :year (:year carattrs)}))) 
                               (:parking-lot parking-lot))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex Clojure data structure that I would like to serialize -
I have a business class which I need to serialize to xml. It has
i need to serialize directory tree. i have no trouble with this type: std::map<
I have an object that I need to serialize in C#, however this object
I have an xml file.i need to serialize to c# class. <Configs> <Services> <Path>
I have an xml, I need to serialize. <Configuration> <Configs> <tester> <test>gabc</test> <test>def</test> </tester>
I have an object graph that I need to serialize to xml and save
I have the following problem: I have a form I need to serialize but
This is the scenario: I have nested classes and need to serialize then in
For my application, I need to serialize data to file using XML, so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.