#|
XML Interface
Note: This is an example file showing concepts of the Integration Kit.
The code itself is not supported and will never be supported.
|#
(in-package :example)
(use-package :oli)
;;---------------------------------------------
;; creating a document
;;---------------------------------------------
(defun ikit-xml-example-create-document ()
(let ((filename "d:/temp/my.xml")
xml-document
root child grandchild1 grandchild2 grandchild3
)
(display "Building XML document")
(setf xml-document (sd-xml-create-document))
;; attach root
(setf root (sd-xml-create-element xml-document "root"))
(sd-xml-document-set-root xml-document root)
;; create a child under root
(setf child (sd-xml-create-element xml-document "child"))
(sd-xml-element-add-child root child)
;; create grandchildren
(setf grandchild1 (sd-xml-create-element xml-document "grandchild1"))
(sd-xml-element-add-child child grandchild1)
;; set attribute
(sd-xml-element-set-attribute grandchild1 "key1" "value1")
(sd-xml-element-set-attribute grandchild1 "key2" "value2")
(setf grandchild2 (sd-xml-create-element xml-document "grandchild2"))
(sd-xml-element-add-child child grandchild2)
(sd-xml-element-set-attribute grandchild2 "id" "grandchild2 - first")
(setf grandchild2 (sd-xml-create-element xml-document "grandchild2"))
(sd-xml-element-add-child child grandchild2)
(sd-xml-element-set-attribute grandchild2 "id" "grandchild2 - second")
(sd-xml-element-set-attribute grandchild2 "key3" "value3")
(setf grandchild3 (sd-xml-create-element xml-document "grandchild3"))
(sd-xml-element-set-text grandchild3 "Text content for grandchild3 element")
(sd-xml-element-add-child child grandchild3)
(display (format nil "Created document : ~%~A" (sd-xml-serialize xml-document)))
(display "--------------------------------------------")
(display (format nil "Writing XML file ~A" filename))
(sd-xml-write-file xml-document filename :pretty-print nil)
(sd-xml-clear-cache)
)
)
(ikit-xml-example-create-document)
;;---------------------------------------------
;; reading a document
;;---------------------------------------------
(defun ikit-xml-example-display-attributes (element)
(let (
(name (sd-xml-element-get-name element) )
(attributes (sd-xml-element-get-attributes element) )
(children (sd-xml-element-get-children element) )
)
(when attributes
(display (format nil "Attributes for ~A :" name))
(dolist (attribute attributes)
(display (format nil " ~A = ~A" attribute (sd-xml-element-get-attribute element attribute)))
)
)
(dolist (child children)
(ikit-xml-example-display-attributes child)
)
)
)
(defun ikit-xml-example-read-document ()
(let ((filename "d:/temp/my.xml")
(filename2 "d:/temp/my2.xml")
xml-document
grandchild
)
(display (format nil "~%--------------------------------------------"))
(display (format nil "Reading XML file ~A" filename))
(setf xml-document (sd-xml-read-file filename))
(display "--------------------------------------------")
(display "Reading the text content of an element")
(setf grandchild (sd-xml-document-select-element xml-document "/root/child/grandchild3"))
(display (format nil "Text content of 'grandchild3' :~% ~A" (sd-xml-element-get-text grandchild)))
(display (format nil "~%--------------------------------------------"))
(display "Parsing the document and displaying all attributes")
(ikit-xml-example-display-attributes (sd-xml-document-get-root xml-document))
(display (format nil "~%--------------------------------------------"))
(display "Reading an attribute")
(setf grandchild (sd-xml-document-select-element xml-document "/root/child/grandchild2[@id='grandchild2 - second']"))
(display (format nil " Value of 'key3' attribute of 'grandchild2' : ~A" (sd-xml-element-get-attribute grandchild "key3")))
(setf grandchild (sd-xml-document-select-element xml-document "/root/child/grandchild1"))
(display (format nil " Value of 'key1' attribute of 'grandchild1' : ~A" (sd-xml-element-get-attribute grandchild "key1")))
(display (format nil "~%--------------------------------------------"))
(display "Modifying an attribute")
(sd-xml-element-set-attribute grandchild "key1" "value_modified")
(display (format nil "Modified document - now it's :~%~A" (sd-xml-serialize xml-document)))
;; write
(display (format nil "--------------------------------------------"))
(display (format nil "Writing XML file ~A" filename2))
(sd-xml-write-file xml-document filename2)
(sd-xml-clear-cache)
(sd-display-url filename2)
)
)
(ikit-xml-example-read-document)
;;