2013-01-06 28 views
1

我是新来的lisp,我似乎无法找到任何如何格式化从txt文件读取单词到xml的例子。Lisp数据格式化到xml

例如:

tag1 
tag2 word2 
tag1 word3 
tag1 word4 

我想有退出文件:.XML

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

或类似的事情。感谢您的任何帮助。

回答

1

随着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> 

你留下什么用是要弄清楚如何处理您代表中元素的嵌套...