随着CXML和SPLIT序列库,你可以做这样的:
(defun write-xml (input-stream output-stream)
(cxml:with-xml-output
(cxml:make-character-stream-sink output-stream
:indentation 2 :canonical nil)
(loop :for line := (read-line input-stream nil) :while line :do
(destructuring-bind (tag &optional text)
(split-sequence:split-sequence #\Space line)
(cxml:with-element tag
(when text
(cxml:text text)))))))
结果会稍有不同:
CL-USER> (with-input-from-string (in "tag1
tag2 word2
tag1 word3
tag1 word4")
(write-xml in *standard-output*))
<?xml version="1.0" encoding="UTF-8"?>
<tag1/>
<tag2>
word2</tag2>
<tag1>
word3</tag1>
<tag1>
word4</tag1>
你留下什么用是要弄清楚如何处理您代表中元素的嵌套...