2009年1月29日木曜日

XMLを扱う (その10) [Common Lisp]

続き。

さて、xmlsからXMLへ。

---
CL-USER(37): (with-open-file (out "example-xmls.out" :direction :output :element-type '(unsigned-byte 8))
(cxml-xmls:map-node (cxml:make-octet-stream-sink out)
'("test" (("a" "b")) ("child" nil))))
Error: serializing with :INCLUDE-NAMESPACE-URI, but node was created without namespace URI

Restart actions (select using :continue):
0: Return to Top Level (an "abort" restart).
1: Abort entirely from this (lisp) process.
[1] CL-USER(38): :reset
CL-USER(39): (with-open-file (out "example-xmls.out" :direction :output :element-type '(unsigned-byte 8))
(cxml-xmls:map-node (cxml:make-octet-stream-sink out)
'("test" (("a" "b")) ("child" nil))
:include-namespace-uri nil))
Error: File #P"example-xmls.out" already exists.
[condition type: FILE-ERROR]

Restart actions (select using :continue):
0: Return to Top Level (an "abort" restart).
1: Abort entirely from this (lisp) process.
[1] CL-USER(40): :reset
CL-USER(41): (with-open-file (out "example-xmls.out" :direction :output :element-type '(unsigned-byte 8)
:if-exists keyword::supersede)
(cxml-xmls:map-node (cxml:make-octet-stream-sink out)
'("test" (("a" "b")) ("child" nil))
:include-namespace-uri nil))
#<EXCL::BINARY-OUTPUT-FILE-STREAM #P"example-xmls.out" closed @ #x100168f642>
CL-USER(42):
---

Quick-Start examples の例がちょっとよろしくない。

sinkって何だろ?

調べる。しかし、整理された説明はない。ストリームの端や間に設置され
て、なんらかの処理をするデータ貯まりと理解しておく。

次。klacks。
---
CL-USER(42): (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 {}}
NIL
CL-USER(43):
---
うーん。SAXに似ている?違うのはconsume?

調べる。

** JavaのStreaming API for XML (StAX)が元ネタ。
** pull-basedである。SAXはpush-basedである。
** なるほど。peekとconsumeがあることによって"pull"によって制御の流
れを書くので、制御しやすいということか。

---
CL-USER(43): (cxml:with-xml-output (cxml:make-octet-stream-sink *standard-output* :indentation 2 :canonical nil)
(cxml:with-element "foo"
(cxml::attribute "xyz" "abc")
(cxml:with-element "bar"
(cxml:attribute "blub" "bla"))
(cxml:text "Hi there.")))
<?xml version="1.0" encoding="UTF-8"?>
<foo xyz="abc">
<bar blub="bla"/>
Hi there.
</foo>
#<TERMINAL-SIMPLE-STREAM [initial terminal io] fd 0/1 @ #x1000245e42>
CL-USER(44):
---

cxmlってやたら充実してるな、、、、
まだつづく。次回は、cxml-stp。

0 件のコメント: