00001 <?php
00002
00016
00017
00018 class XMLNode {
00019
00021 var $name;
00022
00024 var $parent;
00025
00027 var $attributes;
00028
00030 var $value;
00031
00033 var $children;
00034
00039 function XMLNode($name = null) {
00040 $this->name = $name;
00041 $this->parent = null;
00042 $this->attributes = array();
00043 $this->value = null;
00044 $this->children = array();
00045 }
00046
00050 function getName() {
00051 return $this->name;
00052 }
00053
00057 function setName($name) {
00058 $this->name = $name;
00059 }
00060
00064 function &getParent() {
00065 return $this->parent;
00066 }
00067
00071 function setParent(&$parent) {
00072 $this->parent = &$parent;
00073 }
00074
00078 function getAttributes() {
00079 return $this->attributes;
00080 }
00081
00086 function getAttribute($name) {
00087 return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
00088 }
00089
00094 function setAttribute($name, &$value) {
00095 $this->attributes[$name] = &$value;
00096 }
00097
00101 function setAttributes($attributes) {
00102 $this->attributes = $attributes;
00103 }
00104
00108 function &getValue() {
00109 return $this->value;
00110 }
00111
00115 function setValue($value) {
00116 $this->value = &$value;
00117 }
00118
00122 function &getChildren() {
00123 return $this->children;
00124 }
00125
00131 function &getChildByName($name, $index = 0) {
00132 foreach ($this->children as $child) {
00133 if ($child->getName() == $name) {
00134 if ($index == 0) {
00135 return $child;
00136 } else {
00137 $index--;
00138 }
00139 }
00140 }
00141 $child = null;
00142 return $child;
00143 }
00144
00148 function addChild(&$node) {
00149 $this->children[] = &$node;
00150 }
00151
00156 function &toXml($output = null) {
00157 $nullVar = null;
00158 $out = '';
00159
00160 if ($this->parent === null) {
00161
00162 $out .= "<?xml version=\"" . $this->getAttribute('version') . "\" encoding=\"UTF-8\"?>\n";
00163 if ($this->getAttribute('type') != '') {
00164 if ($this->getAttribute('url') != '') {
00165 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " PUBLIC \"" . $this->getAttribute('dtd') . "\" \"" . $this->getAttribute('url') . "\">";
00166 } else {
00167 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " SYSTEM \"" . $this->getAttribute('dtd') . "\">";
00168 }
00169 }
00170 }
00171
00172 if ($this->name !== null) {
00173 $out .= '<' . $this->name;
00174 foreach ($this->attributes as $name => $value) {
00175 $value = XMLNode::xmlentities($value);
00176 $out .= " $name=\"$value\"";
00177 }
00178 $out .= '>';
00179 }
00180 $out .= XMLNode::xmlentities($this->value, ENT_NOQUOTES);
00181 foreach ($this->children as $child) {
00182 if ($output !== null) {
00183 if ($output === true) echo $out;
00184 else fwrite ($output, $out);
00185 $out = '';
00186 }
00187 $out .= $child->toXml($output);
00188 }
00189 if ($this->name !== null) $out .= '</' . $this->name . '>';
00190 if ($output !== null) {
00191 if ($output === true) echo $out;
00192 else fwrite ($output, $out);
00193 return $nullVar;
00194 }
00195 return $out;
00196 }
00197
00198 function xmlentities($string, $quote_style=ENT_QUOTES) {
00199 return htmlspecialchars($string, $quote_style, 'UTF-8');
00200 }
00201
00202 }
00203
00204 ?>