Make sure to install and load cxml first.
Create a test file called example.xml:
* (with-open-file (s "example.xml" :direction :output)
(write-string "<test a='b'><child/></test>" s))
Parse example.xml into a DOM tree (read more):
* (cxml:parse-file "example.xml" (cxml-dom:make-dom-builder)) #<DOM-IMPL::DOCUMENT @ #x72206172> ;; save result for later: * (defparameter *example* *) *EXAMPLE*
Inspect the DOM tree (read more):
* (dom:document-element *example*) #<DOM-IMPL::ELEMENT test @ #x722b6ba2> * (dom:tag-name (dom:document-element *example*)) "test" * (dom:child-nodes (dom:document-element *example*)) #(#<DOM-IMPL::ELEMENT child @ #x722b6d8a>) * (dom:get-attribute (dom:document-element *example*) "a") "b"
Serialize the DOM document back into a file (read more):
(with-open-file (out "example.out" :direction :output :element-type '(unsigned-byte 8)) (dom:map-document (cxml:make-octet-stream-sink out) *example*))
As an alternative to DOM, parse into xmls-compatible list structure (read more):
* (cxml:parse-file "example.xml" (cxml-xmls:make-xmls-builder))
("test" (("a" "b")) ("child" NIL))
Use klacks to read events from the parser incrementally. The following example looks only for :start-element and :end-element events and prints them (read more):
* (klacks:with-open-source
(s (cxml:make-source #p"example.xml"))
(loop
for key = (klacks:peek s)
while key
do
(case key
(:start-element
(format t "~A {" (klacks:current-qname s)))
(:end-element
(format t "}")))
(klacks:consume s)))
test {child {}}