J2EE之巔

           

          Clojure XPath

          The functions to support using XPath in Clojure.

          Source Code

           1 ;The code was implemented by caichao@amazon.com
           2 ;You could use the code anyway, but should keep the comments
           3 ;Created 2012.10
           4 (ns clojure.ccsoft.xml
           5   (:require [clojure.xml :as xml]))
           6  
           7 (import '(java.io StringReader)
           8         '(java.io ByteArrayInputStream))
           9  
          10 (defn xml-structure [xml-txt] 
          11    [ (xml/parse (-> xml-txt
          12               (.getBytes)
          13               (ByteArrayInputStream.)
          14      )
          15     )]
          16 )
          17  
          18 (defn node [tag xmlStruct]
          19  
          20   (first (filter #(= (:tag %) tag) (:content xmlStruct)))
          21 )
          22  
          23 (defn node [path xml-txt]
          24    (loop [path path 
          25           xml-content (xml-structure xml-txt) 
          26           ]
          27       (let [current-tag (first path) current-elem (first xml-content)]
          28         (if (= (:tag current-elem ) current-tag)
          29  
          30           (if (= (count path) 1)
          31             current-elem 
          32             (recur  (rest path) (:content current-elem ))
          33           )
          34           (if (> (count  xml-content) 1)
          35            (recur path  (rest xml-content))
          36           )
          37         )
          38      )
          39     )
          40  )

          How to Use

          (def cmd-example "<command>
                             <header>
                               
          <type>script</type>
                               
          <transaction_id>12345</transaction_id>
                             
          </header>
                             
          <body>
                                println 
          3+4;
                             
          </body>
                            
          </command>")
           
           
          (node [:command :header :transaction_id] cmd
          -example)


          posted on 2012-10-15 10:15 超越巔峰 閱讀(2877) 評論(0)  編輯  收藏 所屬分類: Clojure


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(12)

          隨筆分類(54)

          隨筆檔案(59)

          文章分類(2)

          文章檔案(1)

          相冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 诸城市| 新丰县| 冕宁县| 拜泉县| 新乡县| 宁化县| 巴彦县| 建水县| 海城市| 南部县| 台东县| 萝北县| 郸城县| 天峻县| 秀山| 寻乌县| 资中县| 旬阳县| 长顺县| 雷山县| 阳山县| 安阳市| 黑龙江省| 新巴尔虎右旗| 察隅县| 华亭县| 绵竹市| 阳信县| 盘山县| 阿勒泰市| 锦屏县| 禹城市| 崇信县| 团风县| 临猗县| 襄城县| 青神县| 岐山县| 内黄县| 恩施市| 隆昌县|