|
| 1 | +;;; xml-to-string --- convert lisp s-expressions to xml string |
| 2 | + |
| 3 | +;; Copyright (C) 2012 Dave Paroulek |
| 4 | + |
| 5 | +;; Author: Dave Paroulek <[email protected]> |
| 6 | +;; Maintainer: Dave Paroulek <[email protected]> |
| 7 | +;; Version: 0.1.0 |
| 8 | +;; Keywords: xml, data |
| 9 | +;; URL: http://github.com/upgradingdave/xml-to-string |
| 10 | + |
| 11 | +;; This file is NOT part of GNU Emacs. |
| 12 | + |
| 13 | +;;; License: |
| 14 | + |
| 15 | +;; This program is free software; you can redistribute it and/or modify |
| 16 | +;; it under the terms of the GNU General Public License as published by |
| 17 | +;; the Free Software Foundation; either version 3, or (at your option) |
| 18 | +;; any later version. |
| 19 | + |
| 20 | +;; This program is distributed in the hope that it will be useful, |
| 21 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | +;; GNU General Public License for more details. |
| 24 | + |
| 25 | +;; You should have received a copy of the GNU General Public License |
| 26 | +;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 27 | +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 28 | +;; Boston, MA 02110-1301, USA. |
| 29 | + |
| 30 | +;;; Commentary: |
| 31 | + |
| 32 | +;; Functions like xml-parse-file and xml-parse-region output s-expressions |
| 33 | +;; like this: |
| 34 | +;; |
| 35 | +;; xml-list ::= (node node ...) |
| 36 | +;; node ::= (qname attribute-list . child_node_list) |
| 37 | +;; child_node_list ::= child_node child_node ... |
| 38 | +;; child_node ::= node | string |
| 39 | +;; qname ::= (:namespace-uri . "name") | "name" |
| 40 | +;; attribute_list ::= ((qname . "value") (qname . "value") ...) |
| 41 | +;; | nil |
| 42 | +;; string ::= "..." |
| 43 | + |
| 44 | +;; The functions below take s-exp's like these and output them back into xml strings |
| 45 | + |
| 46 | +;;; Code: |
| 47 | + |
| 48 | +(defun xml-to-string (node-list) |
| 49 | + "Convert sexp created by xml-parse-* function back to xml string" |
| 50 | + (let ((result)) |
| 51 | + (dolist (node node-list result) |
| 52 | + (setq result (concat result (xml-to-string-parse-node node)))))) |
| 53 | + |
| 54 | +(defun xml-to-string-parse-node (node) |
| 55 | + "Convert `(qname attribute-list . child_node_list)` to xml string representation" |
| 56 | + (concat "<" (xml-to-string-parse-qname (symbol-name (car node))) |
| 57 | + (if (cadr node) " ") |
| 58 | + (xml-to-string-parse-attr-list (cadr node)) ">" |
| 59 | + (xml-to-string-parse-child-node-list (cddr node)) |
| 60 | + "</" (xml-to-string-parse-qname (symbol-name (car node))) ">")) |
| 61 | + |
| 62 | +(defun xml-to-string-parse-child-node-list (child-node-list) |
| 63 | + (let (value) |
| 64 | + (dolist (node child-node-list value) |
| 65 | + (setq value (concat value (xml-to-string-parse-child-node node)))))) |
| 66 | + |
| 67 | +(defun xml-to-string-parse-child-node (child-node) |
| 68 | + "Convert `node | string' to string representation" |
| 69 | + (if (stringp child-node) |
| 70 | + child-node |
| 71 | + (xml-to-string-parse-node child-node))) |
| 72 | + |
| 73 | +(defun xml-to-string-parse-qname (node) |
| 74 | + "Currently just assumes it's name, need to implement namespace url" |
| 75 | + node) |
| 76 | + |
| 77 | +(defun xml-to-string-parse-attr-list (attr-list) |
| 78 | + "Convert `((qname . \"value\") (qname . \"value\") ...)' to a |
| 79 | + string like \"qname=value qname=value\" ..." |
| 80 | + (mapconcat (lambda (elem) (concat (symbol-name (car elem)) |
| 81 | + "=" |
| 82 | + "\"" (cdr elem) "\"")) attr-list " ")) |
| 83 | + |
| 84 | + |
| 85 | +(provide 'xml-to-string) |
| 86 | + |
0 commit comments