2

I am trying to convert the following maps to xml (any key with a vector value needs to repeat the key in the xml for each element in the vector)

(use 'clojure.xml)
(defn map-to-xml2 [k v]
      (cond
         (nil? k)    (for [[e a] v] {:tag e :content (map-to-xml2 e a)})
         (map? v)    (for [[e a] v] {:tag e :content (map-to-xml2 e a)})
         (vector? v) (for [x v] {:tag k :content (for [[e a] x] {:tag e :content (map-to-xml2 e a)})})
         :else       [(str v)]))

(def studios [{:company {:name "Acme Corp" :id 1 :rank 20 :employee 
                  [{:fname "Mark" :lname "Jones"} {:fname "Leroy" :lname "Bell"}]}}
              {:company {:name "Eastwood Studios" :id 2 :rank 35 :employee 
                  [{:fname "Lee" :lname "Marvin"} {:fname "Clint" :lname "Eastwood"}]}}])

(->> studios first (map-to-xml2 nil) first emit with-out-str (spit "acme.xml"))
(->> studios second (map-to-xml2 nil) first emit with-out-str (spit "eastwood.xml"))

I get the following xml

<?xml version='1.0' encoding='UTF-8'?>
<company>
  <rank>35</rank>
  <employee>
    <employee>
      <lname>Marvin</lname>
      <fname>Lee</fname>
    </employee>
    <employee>
      <lname>Eastwood</lname>
      <fname>Clint</fname>
    </employee>
  </employee>
  <name>Eastwood Studios</name>
  <id>2</id>
</company>

when what i really need to send over via soap is

<?xml version='1.0' encoding='UTF-8'?>
<company>
  <name>Eastwood Studios</name>
  <id>2</id>  
  <rank>35</rank>
  <employee>
    <lname>Marvin</lname>
    <fname>Lee</fname>
  </employee>
  <employee>
    <lname>Eastwood</lname>
    <fname>Clint</fname>
  </employee>
</company>

How do i correct the above?

I am trying to read data from excel file and for each row or group of rows with identical id make webservice call, then update the spreadsheet with the response. The above is to generate the xml needed for the webservice call.

2
  • I don't know a lot about xml. I see two differences between what you currently have, and what you want: the double-nesting of the "employee" tag, and order. Do you need to fix both of these things or just the double-nesting (or, as is always possible, is it something different entirely)? Jul 31, 2012 at 4:01
  • @omri just the double nesting would suffice. the order would be nice to have. Jul 31, 2012 at 11:55

2 Answers 2

1

Your main problem, which you probably already know, is that when the content (i.e. v in the program body) is a vector, you have to do some sort of (for ...) or (map ...) expression to truly express all of the tags and content of it. However, in so doing, you generate a sequence of tags, which is packaged inside pesky parens. From what I can tell, you would need to "unparens" those in order to obtain the correct structure to pass to (emit-element ...).

Hence in my code below, the expression (mapcat to-xml ...) is in the places where nesting is necessary, because this will perform the operation down successive items, and then concatenate them all together. Unfortunately, you then have to put what used to be single item returns inside vectors (or lists if you wanted). That is why when (map? v) is true or when :else happens, the whole (tag-xml ...) expression is wrapped in a vector. Any returns will be concated with other returns.

I think I've found something that will work for you. It's not great, in my opinion, because I don't like how it handles the top-level call--i.e. the call you would make in your code (but I'll get to that later):

(defn tag-xml
  [tag content]
  {:tag tag
   :content content})

(defn to-xml
  ([[k v]] ;//This form of to-xml is for the sake of nested calls
    (cond
      (map? v) [(tag-xml k (mapcat to-xml v))]
      (vector? v) (for [x v] (tag-xml k (mapcat to-xml x)))
      :else [(tag-xml k [(str v)])]))
  ([k v] ;//This form of to-xml is only for the sake of the top level call
    (tag-xml k (if (map? v) (mapcat to-xml v) [(str v)]))))

Notice that I added a helper function tag-xml. This was just to make the body of to-xml cleaner and smaller.

This is how you might use it (although in your case, you would replace println with some spit call):

=> (->> studios ffirst (apply to-xml) emit with-out-str println))
<?xml version='1.0' encoding='UTF-8'?>
<company>
<rank>
20
</rank>
<employee>
<lname>
Jones
</lname>
<fname>
Mark
</fname>
</employee>
<employee>
<lname>
Bell
</lname>
<fname>
Leroy
</fname>
</employee>
<name>
Acme Corp
</name>
<id>
1
</id>
</company>
=> nil

So, I don't like that in order to call it from the top level properly, to some existing hash-map data, you would need to do (apply to-xml (first data)). You could get around this by instead of having your data as a hash-map, structure it as a vector. In you example this would look like [:company ...] instead of {:company ...} for each studio in studios. Then instead, you could use the function like this: (first (to-xml data)).

Still, this isn't as elegant as I would like it to be. Maybe the solution would be to have some function to-xml that would do the top level call, and some other function -to-xml that would handle it after that. As a user, you would only be using to-xml but all the hard work would be being done in -to-xml. I'm not crazy about that idea either. Yet another idea would be do something somewhat like what you did, where if the first argument equals nil then it does the function as though it were a top level call. Hmm.

Anyways, it works, so that's something.

Edit
As for wanting to preserve the order, you would probably have to either redefine your data, or transform it before processing it with to-xml. You can not rely on the order of anything written as {...}. If you want to keep it as a map, then you could get order by making it an array map or a sorted map.

If you were to redefine it to make it an array map, it would look something like this:

(def studios [(array-map :company (array-map :name "Acme Corp" :id 1 :rank 20
                         :employee [(array-map :fname "Mark" :lname "Jones")
                                    (array-map :fname "Leroy" :lname "Bell")]))
              (array-map :company (array-map :name "Eastwood Studios" :id 2 :rank 35
                         :employee [(array-map :fname "Lee" :lname "Marvin")
                                    (array-map :fname "Clint" :lname "Eastwood")]))])

Basically, anywhere you used to have {...} you now have (array-map ...). At this point, I should say, don't bother try writing a macro to do that for you, it won't work (see here for my question on this). If you want to go with using a sorted map, you will have to make a predicate comparator that just returns true or false based on some hard-coded ordering, and this seems a little odd to me.

Now, if you want to transform the data, you will need another data structure that contains key orders as well as nested orders. Something like:

(def studio-order-specs {:company [:name :id :rank {:employee [:lname :fname:]}]})

I don't have the transforming function at hand, but using this data structure, you should be able to write something that converts a hash map to an array map of the specified ordering. (You could also use this to convert to a sorted map of the specified ordering, but again, it would be through defining a predicate in an inelegant way--in my opinion.)

1

your input has one extra layer of nesting with the list of employees under the :employee keyword in a map. If you change that enclosing structure to a list you can flatten the whole tree by one level.

{:company {:name "Acme Corp" :id 1 :rank 20 :employee 
              [{:fname "Mark" :lname "Jones"} {:fname "Leroy" :lname "Bell"}]}}

becomes something like:

{:company [{:name "Acme Corp"} 
           {:id 1} {:rank 20} 
           {:fname "Mark" :lname "Jones"} 
           {:fname "Leroy" :lname "Bell"}]}
1
  • if i take that level out there is no way for me to identify the node name. Jul 31, 2012 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.