3 Copyright (C) 2006 Google Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * Class used to generate XML data
24 * Based on sample code available at http://simon.incutio.com/code/php/XmlWriter.class.php.txt
36 * @param string $indent
38 function XmlBuilder($indent = ' ') {
39 $this->indent
= $indent;
40 $this->xml
= '<?xml version="1.0" encoding="utf-8"?>' . "\n";
44 for ($i = 0, $j = count($this->stack
); $i < $j; $i++
) {
45 $this->xml
.= $this->indent
;
49 //Used when an element has sub-elements
50 // This function adds an open tag to the output
53 * @param array $attributes
55 function Push($element, $attributes = array()) {
57 $this->xml
.= '<' . $element;
58 foreach ($attributes as $key => $value) {
59 $this->xml
.= ' ' . $key . '="' . htmlentities($value) . '"';
62 $this->stack
[] = $element;
65 //Used when an element has no subelements.
66 //Data within the open and close tags are provided with the
71 * @param array $attributes
73 function Element($element, $content, $attributes = array()) {
75 $this->xml
.= '<' . $element;
76 foreach ($attributes as $key => $value) {
77 $this->xml
.= ' ' . $key . '="' . htmlentities($value) . '"';
79 $this->xml
.= '>' . htmlentities($content) . '</' . $element . '>' . "\n";
84 * @param array $attributes
86 function EmptyElement($element, $attributes = array()) {
88 $this->xml
.= '<' . $element;
89 foreach ($attributes as $key => $value) {
90 $this->xml
.= ' ' . $key . '="' . htmlentities($value) . '"';
92 $this->xml
.= " />\n";
95 //Used to close an open tag
99 function Pop($pop_element) {
100 $element = array_pop($this->stack
);
102 if ($element !== $pop_element)die('XML Error: Tag Mismatch when trying to close "' . $pop_element . '"');
103 else $this->xml
.= "</$element>\n";
110 if (count($this->stack
) != 0)die('XML Error: No matching closing tag found for " ' . array_pop($this->stack
) { . '"'
113 else return $this->xml
;