Back

content (clj)

(source)

variable

Examples

clojure

(ns clojure.test-clojure.clojure-xml
  (:use clojure.test)
  (:require [clojure.xml :as xml])
  (:import [java.io ByteArrayInputStream]))

(deftest CLJ-2611-avoid-XXE
  (let [xml-str "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<!DOCTYPE foo [
  <!ELEMENT foo ANY >
  <!ENTITY xxe SYSTEM \"file:///etc/hostname\" >]>
<foo>&xxe;</foo>"]
    (is (= {:tag :foo, :attrs nil, :content nil}
           (with-open [input (ByteArrayInputStream. (.getBytes xml-str))]
             (xml/parse input))))))
; parse
bhb/expound
(require '[clojure.xml :as xml] )

(let [deps (->> (xml/parse "../pom.xml")                               ; read pom
                :content                                               ; get top-level tags
                (filter #(= (:tag %) :dependencies))                   ; find dependencies
                first                                                  ; get tag
                :content                                               ; get children
                (map :content)                                         ; get children's children
                (remove (fn [dep-tags]                                 ; find anything not in 'test' scope
                          (some #(and (= (:tag %) :scope)
                                      (= (:content %) ["test"])) dep-tags)))
                (map (fn [dep-tags]                                    ; pull out group/name of dependency
             [(:content
                 (first
                  (filter #(= (:tag %) :groupId) dep-tags)))
              (:content
                (first
                 (filter #(= (:tag %) :artifactId) dep-tags)))]))
reborg/clojure-essential-reference
(require '[clojure.java.io :as io])
(require '[clojure.xml :as xml])

(filter (comp string? first :content) (xml-seq xml)) ; <2>

;; ({:tag :accountId, :attrs nil, :content ["3764882"]}
    {:tag :currentBalance, :attrs nil, :content ["80.12389"]}
    {:tag :contractId, :attrs nil, :content ["77488"]}
    {:tag :currentBalance, :attrs nil, :content ["1921.89"]})
reborg/clojure-essential-reference
(require '[clojure.java.io :as io])
(require '[clojure.xml :as xml])

(filter (comp string? first :content) (xml-seq xml)) ; <2>

;; ({:tag :accountId, :attrs nil, :content ["3764882"]}
    {:tag :currentBalance, :attrs nil, :content ["80.12389"]}
    {:tag :contractId, :attrs nil, :content ["77488"]}
    {:tag :currentBalance, :attrs nil, :content ["1921.89"]})