我得到了以下XML
<EXAMPLE>
<WANNAPRINT>
AWESOME
</WANNAPRINT>
<DONTWANNAPRINT>
BAD
</DONTWANNAPRINT>
</EXAMPLE>我使用CXML-STP处理给定的XML,我只想打印"wannaprint“部分。
我正试图使用序列化来实现这一点:
(cxml-stp:serialize
(let ((last-step
(car
(cxml-stp:filter-children #'(lambda(node)
(and (typep node 'stp:element)
(string= (stp:local-name node)
"WANNAPRINT")))
(cxml-stp:first-child
(cxml:parse
"<EXAMPLE>
<WANNAPRINT>
AWESOME
</WANNAPRINT>
<DONTWANNAPRINT>
BAD
</DONTWANNAPRINT>
</EXAMPLE>" (stp:make-builder)))))))
(FORMAT t "last-step:~a~%" last-step)
last-step)
(cxml:make-string-sink))但结果只是“零”,我不明白为什么,因为打印的“最后一步”变量显示,正确的元素已经被选中。
如何将cxml-stp:文档的元素序列化/打印成字符串?
发布于 2014-05-01 22:08:16
根据cxml:make-string-sink的文档,字符串接收器返回它们的字符串是调用sax:end-document的结果,所以您可以做的一件事是:
(let ((sink (cxml:make-string-sink)))
(cxml-stp:serialize
(let ((last-step
(car
(cxml-stp:filter-children #'(lambda(node)
(and (typep node 'stp:element)
(string= (stp:local-name node)
"WANNAPRINT")))
(cxml-stp:first-child
(cxml:parse
"<EXAMPLE> <WANNAPRINT> AWESOME </WANNAPRINT> <DONTWANNAPRINT> BAD </DONTWANNAPRINT> </EXAMPLE>"
(stp:make-builder)))))))
(FORMAT t "last-step:~a~%" last-step)
last-step)
sink)
(sax:end-document sink))这似乎有点令人讨厌,但似乎没有导出的机制来获取水池内的Y流。
另一种方法是将element (即last-step)放入document中。我认为它和(stp:make-document last-step)一样简单,但它抱怨说元素已经有了父元素,我现在不记得该如何绕过它了。无论如何,您可能不需要文档,因为它的序列化包括序曲(<?xml.?>)。
https://stackoverflow.com/questions/23393395
复制相似问题