1

私はlispを初めて使用し、読み取り単語をtxtファイルからxmlにフォーマットする方法の例を見つけることができないようです。

例:

tag1
tag2 word2
tag1 word3
tag1 word4

終了ファイルが欲しいのですが:.xml

<tag1>
   <tag2>word2</tag2>
</tag1>
<tag1>word3</tag1>
<tag1>word4</tag1>

または同様のもの。

4

1 に答える 1

1

CXML および SPLIT-SEQUENCE ライブラリを使用すると、次のように実行できます。

(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>

残っているのは、表現内の要素のネストを処理する方法を理解することです...

于 2013-01-06T14:29:26.033 に答える