Regarding XML and recursive tree transformation

clojure

#1

Hello everyone,

The background here is that, I’ve been hoping to get some projects done in Clojure for quite some time now and when the opportunity regarding the XML tree transformation presented itself - I jumped at it.

However, after trying many libraries like clojure.xml , clojure.zip.data.xml or xml-in - I haven’t really been able to successfully complete the roundtrip from mangeto1 XML -> android XML and back from android XML to mangento1 XML.

I have prepared a sample repo from scratch, regarding the same on Github here https://github.com/abhi18av/xml-transformer/blob/master/doc/magento1-android-xml-transformer.md

Could you please suggest how to go about this task ?


#2

Hi @abhi18av,

I’m not familiar with either the Android or Magento XML format. Your magenta example in particular seems to have some xml inside xml, encoded as a string. Is that really how it’s supposed to look?

I would urge you to look at enlive, it allows you to deal with xml using selectors not unlike CSS or XPath, except that selectors are just clojure vectors, e.g. [:body :#app :li].

Unfortunately the documentation to enlive is pretty terrible. The README is an absolute mess (skip the first several pages until you get to the section titled “older stuff”), and most docs you find focus on using it as a templating system. The main namespace is called net.cgrand.enlive-html although it just deals with Clojure xml data, …

I would look at these sections of the README: selectors, the at form, transformations, as well as the select function. Keep the API docs handy as well.

There are several functions that will parse html/xml, although if you’re dealing with xml I would use clojure.xml/parse, the return value of that is perfectly suitable for use with select, at etc.

I hope that helps. If you’re still stuck I can write up a small example later.


#3

Thanks Arne!

I have since you response, started to explore enlive and I am slowly making my way through it’s API, trying to understand how I can apply it in my use case.

I never related it with XML but I’m happy to note that many methods just work in XML just fine and there’s even a namespace within enlive for XML !


#4

@plexus, is there a way to query clojure data structures much like what enlive is doing for the html and `xml?

I have a hunch that datomic-free version could be used for achieving the same and previously, I’ve used lucid.query and lucid.mind to achieve much the same result. Still, I’d like to know the community’s opinion on this.


#5

I’m not sure how similar it is, but Specter may be helpful.


#6

Hmm, let me have a look at Specter then. Thanks @shaun


#7

This wiki page will help you navigate the specter API (proudly contributed by me :wink:)


#8

I have been using that wiki page a lot, it is very useful. Thanks! :slight_smile: