Back
parse (clj)
(source)function
(parse s)
(parse s startparse)
Parses and loads the source s, which can be a File, InputStream or
String naming a URI. Returns a tree of the xml/element struct-map,
which has the keys :tag, :attrs, and :content. and accessor fns tag,
attrs, and content. Other parsers can be supplied by passing
startparse, a fn taking a source and a ContentHandler and returning
a parser.
Prior to 1.11, used startparse-sax by default. As of 1.11, uses
startparse-sax-safe, which disables XXE (XML External Entity)
processing. Pass startparse-sax to revert to prior behavior.
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])
(def xml (-> balance .getBytes io/input-stream xml/parse)) ; <1>
reborg/clojure-essential-reference
(require '[clojure.java.io :as io])
(require '[clojure.xml :as xml])
(def xml (-> balance .getBytes io/input-stream xml/parse)) ; <1>