72 var $namespaces = array();
88 var $use_cdata_section = null;
99 function XML_Tree_Node($name, $content =
'', $attributes = array(), $lineno = null, $use_cdata_section = null)
102 if (PEAR::isError($check_name)) {
103 $this->error =& $check_name;
107 if (!is_array($attributes)) {
108 $attributes = array();
111 foreach ($attributes as $attribute_name => $value) {
113 if (PEAR::isError($error)) {
114 $this->error =& $error;
119 $this->
setContent($content, $use_cdata_section);
120 $this->attributes = $attributes;
121 $this->children = array();
122 $this->lineno = $lineno;
138 function &
addChild($child, $content =
'', $attributes = array(), $lineno = null, $use_cdata_section = null)
140 $index =
sizeof($this->children);
142 if (is_object($child)) {
143 if (strtolower(get_class($child)) ==
'xml_tree_node') {
144 $this->children[$index] = $child;
147 if (strtolower(get_class($child)) ==
'xml_tree' && isset($child->root)) {
148 $this->children[$index] = $child->root->getElement();
151 $node =
new XML_Tree_Node($child, $content, $attributes, $lineno, $use_cdata_section);
152 if (PEAR::isError($node->error)) {
156 $this->children[$index] = $node;
159 return $this->children[$index];
172 $clone =
new XML_Tree_Node($this->name,$this->content,$this->attributes);
174 $max_child=count($this->children);
175 for($i=0;$i<$max_child;$i++) {
176 $clone->children[]=$this->children[$i]->cloneNode();
209 function &
insertChild($path,$pos,&$child, $content =
'', $attributes = array())
212 if (PEAR::isError($parent)) {
215 } elseif ($parent != $this) {
217 return $parent->insertChild(null, $pos, $child, $content, $attributes);
220 if (($pos < -count($this->children)) || ($pos > count($this->children))) {
221 return new PEAR_Error(
"Invalid insert position.");
224 if (is_object($child)) {
226 if (strtolower(get_class($child)) ==
'xml_tree_node') {
227 array_splice($this->children, $pos, 0,
'dummy');
229 $pos = count($this->children) + $pos - 1;
231 $this->children[$pos] = &$child;
233 } elseif (strtolower(get_class($child)) ==
'xml_tree' && isset($child->root)) {
234 array_splice($this->children, $pos, 0,
'dummy');
236 $pos = count($this->children) + $pos - 1;
238 $this->children[$pos] = $child->root;
240 return new PEAR_Error(
"Bad node (must be a XML_Tree or an XML_Tree_Node)");
243 array_splice($this->children, $pos, 0,
'dummy');
245 $pos = count($this->children) + $pos - 1;
247 $this->children[$pos] =
new XML_Tree_Node($child, $content, $attributes);
265 if (($pos < -count($this->children)) || ($pos >= count($this->children))) {
266 return new PEAR_Error(
"Invalid remove position.");
270 return array_splice($this->children, $pos, 1);
283 $this->
namespace[$name] = $path;
294 function &
get($use_cdata_section =
false)
297 static $do_ident =
true;
300 $ident = str_repeat(
' ', $deep);
301 if ($this->name !== null) {
303 $out = $ident .
'<' . $this->name;
305 $out =
'<' . $this->name;
307 foreach ($this->attributes as $name => $value) {
308 $out .=
' ' . $name .
'="' . $value .
'"';
311 if (isset($this->
namespace) && (is_array($this->
namespace))) {
312 foreach ($this->
namespace as $qualifier => $uri) {
313 if ($qualifier ==
'') {
314 $out .=
" xmlns='$uri'";
316 $out .=
" xmlns:$qualifier='$uri'";
321 if ($this->content ==
'' &&
sizeof($this->children) === 0 && $deep != 0) {
326 if ($this->use_cdata_section ==
true || ($use_cdata_section ==
true && $this->use_cdata_section !==
false)) {
327 if (trim($this->content) !=
'') {
328 $out .=
'<![CDATA[' .$this->content.
']]>';
331 if (trim($this->content) !=
'') {
332 $out .= $this->content;
337 if (
sizeof($this->children) > 0) {
339 foreach ($this->children as $child) {
340 $out .= $child->get($use_cdata_section);
346 if ($do_ident && $empty !=
true) {
347 $out .= $ident .
'</' . $this->name .
">\n";
348 } elseif ($empty !=
true) {
349 $out .=
'</' . $this->name .
'>';
353 if ($this->use_cdata_section ==
true || ($use_cdata_section ==
true && $this->use_cdata_section !==
false)) {
354 if (trim($this->content) !=
'') {
355 $out = $ident .
'<![CDATA[' .$this->content.
']]>' .
"\n";
358 if (trim($this->content) !=
'') {
359 $out = $ident . $this->content .
"\n";
378 if (isset($this->attributes[$name])) {
379 return $this->attributes[$name];
395 $this->attributes[$name] = $value;
408 if (isset($this->attributes[$name])) {
409 unset($this->attributes[$name]);
423 $this->use_cdata_section = $use_cdata_section;
425 if ($use_cdata_section ==
true) {
426 $this->content = $content;
446 if (!is_array($path)) {
447 $path = array($path);
449 if (
sizeof($path) == 0) {
454 $next = array_shift($path1);
455 if (isset($this->children[$next])) {
456 $x =& $this->children[$next]->getElement($path1);
457 if (!PEAR::isError($x)) {
462 return new PEAR_Error(
"Bad path to node: [".implode(
'-', $path).
"]");
482 if (is_string($path))
483 $path = explode(
"/", $path);
485 if (
sizeof($path) == 0) {
490 $next = array_shift($path1);
494 for ($i = 0; $i < count($this->children); $i++) {
495 if ($this->children[$i]->name == $next) {
496 $child =& $this->children[$i];
500 if (!is_null($child)) {
501 $x =& $child->getNodeAt($path1);
502 if (!PEAR::isError($x)) {
508 return new PEAR_Error(
"Bad path to node: [".implode(
'/', $path).
"]");
522 $xml = str_replace(array(
'ü',
'Ü',
'ö',
528 array(
'ü',
'Ü',
'ö',
529 'Ö',
'ä',
'Ä',
537 $xml = preg_replace(array(
"/\&([a-z\d\#]+)\;/i",
539 "/\#\|\|([a-z\d\#]+)\|\|\#/i",
540 "/([^a-zA-Z\d\s<>\&\;\.\:\=\"\-\/\%\?\!\'\(\)\[\]\{\}\$\#\+\,\@_])/e"
545 "'&#'.ord('\\1').';'"
564 static $trans_tbl = null;
566 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
567 $trans_tbl = array_flip($trans_tbl);
569 for ($i = 1; $i <= 255; $i++) {
570 $ent = sprintf(
"&#%03d;", $i);
572 $xml = str_replace($ent, $ch, $xml);
575 return strtr($xml, $trans_tbl);