Last active
December 17, 2015 06:39
-
-
Save pallix/5567467 to your computer and use it in GitHub Desktop.
Example of using the Clojure Lacij library to display the content of an XML file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns lacij.examples.display-xml | |
(:use lacij.edit.graph | |
lacij.view.graphview | |
lacij.layouts.layout | |
clojure.pprint) | |
(:require [clojure.xml :as xml])) | |
(defn node-text | |
[xml] | |
(if (string? xml) | |
xml | |
(name (:tag xml)))) | |
(defn get-children | |
[xml] | |
(if (string? (:content xml)) | |
() | |
(:content xml))) | |
(defn add-children | |
[g children parent-id] | |
(reduce (fn [g child] | |
(let [node-id (genid)] | |
(-> g | |
(add-node node-id (node-text child)) | |
(add-edge (geneid) node-id parent-id) | |
(add-children (get-children child) node-id)))) | |
g | |
children)) | |
(defn build-graph | |
[xml] | |
(let [g (graph :width 400 :height 400) | |
nid (genid) | |
g (add-node g nid (node-text xml))] | |
(-> g | |
(add-children (:content xml) nid) | |
(layout :hierarchical) | |
(build)))) | |
(defn -main [] | |
(let [xml (clojure.xml/parse "/tmp/t1.xml") | |
g (build-graph xml)] | |
(export g "/tmp/xml.svg"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment