MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mxmltree.class
Ir para a documentação deste ficheiro.
1<?php
7{
8}
9
15{
19 var $tag = '';
20
24 var $value = '';
25
29 var $class = '';
30
34 var $attrs;
35
39 var $properties = array();
40
41}
42
48{
52 var $stack;
53
57 var $top = 0;
58
62 var $tree;
63
64
74 function __construct($file='')
75 {
76 $this->tree = null;
77 $this->stack = array(); // this keeps track of what tag level you're at
78 $doc = new domDocument();
79 $doc->preserveWhiteSpace = false;
80 $doc->load($file);
81 $root = $doc->documentElement;
82 $this->Parse($doc);
83 }
84
94 function Push($node)
95 {
96 $this->stack[++$this->top] = $node;
97 }
98
106 function Pop()
107 {
108 return $this->stack[$this->top--];
109 }
110
118 function Top()
119 {
120 return $this->stack[$this->top];
121 }
122
130 function Count()
131 {
132 return $this->top;
133 }
134
146 function AddProperty($index, $value, $key=NULL)
147 {
148
149 $v = ($key != NULL) ? array($key=>$value) : $value;
150 if (isset($this->stack[$this->top]->properties[$index]))
151 {
152 $p = $this->stack[$this->top]->properties[$index];
153 $p = is_array($p) ? $p : array($p);
154 $v = is_array($v) ? $v : array($v);
155 $this->stack[$this->top]->properties[$index] = array_merge($p,$v);
156 }
157 else
158 {
159 $this->stack[$this->top]->properties[$index] = $v;
160 }
161 }
162
173 function AddPropertyArray($index, $value)
174 {
175 $p = $this->stack[$this->top]->properties[$index];
176 if (isset($p))
177 {
178 if(!is_array($p))
179 {
180 $p = array($p);
181 }
182 $this->stack[$this->top]->properties[$index] = array_merge($p, array($value));
183 }
184 else
185 $this->stack[$this->top]->properties[$index] = array($value);
186 }
187
198 function AddAttribute($attr,$value)
199 {
200 $this->stack[$this->top]->$attr = $value;
201 }
202
212 function SetValue($data)
213 {
214 $this->stack[$this->top]->value = $data;
215 }
216
224 function getTree()
225 {
226 return $this->tree;
227 }
228
239 function _startElement($name, $attrs)
240 {
241 $node = new MXMLNode;
242 $node->tag = $name;
243 $node->attrs = $attrs;
244 $this->Push($node);
245 }
246
256 function _endElement($name)
257 {
258 $node = $this->Pop();
259 // Is there a parent node?
260 if($this->Count())
261 {
266 $node->class = isset($node->attrs['class']) ? $node->attrs['class'] : $node->class;
267 $name = isset($node->attrs['name']) ? $node->attrs['name'] : $node->tag;
268 $type = isset($node->attrs['type']) ? $node->attrs['type'] : '';
269 $key = isset($node->attrs['key']) ? $node->attrs['key'] : NULL;
270 if ($node->tag == 'class')
271 $this->AddAttribute('class', $node->value);
272 elseif (!count($node->properties))
273 {
274 if ($node->value == 'true')
275 $node->value = TRUE;
276 elseif ($node->value == 'false')
277 $node->value = FALSE;
278 $this->AddProperty($name, $node->value, $key);
279 }
280 elseif (($node->class=='array') || ($type == 'array'))
281 {
282 $this->AddProperty($name, $node->properties['item'], $key);
283 }
284 elseif ($node->class=='associative')
285 {
286 $this->AddProperty($name, $node->properties);
287 }
288 else
289 {
290 $this->AddProperty($name, $node,$key);
291 }
292 }
293 else $this->tree = $node;
294 }
295
305 function _characterData($data)
306 {
307 $this->SetValue($data);
308 }
309
319 function Parse($DomNode)
320 {
321 $HasTag = null;
322 if ($ChildDomNode = $DomNode->firstChild) {
323 static $depth = 0;
324 while ($ChildDomNode) {
325 if ($ChildDomNode->nodeType == XML_TEXT_NODE) {
326 $this->_characterData(utf8_decode(trim($ChildDomNode->nodeValue)));
327 } elseif ($ChildDomNode->nodeType == XML_ELEMENT_NODE) {
328 $HasTag = 1;
329 $attrs = array();
330 if ($ChildDomNode->hasAttributes()) {
331 $Array = $ChildDomNode->attributes;
332 foreach ($Array AS $DomAttribute) {
333 $attrs[$DomAttribute->name] = $DomAttribute->value;
334 }
335 }
336 $this->_startElement(utf8_decode($ChildDomNode->nodeName), $attrs);
337
338 if ($ChildDomNode->hasChildNodes()) {
339 $depth++;
340 $this->Parse($ChildDomNode);
341 $depth--;
342 }
343 $this->_endElement(utf8_decode($ChildDomNode->nodeName));
344 }
345 $ChildDomNode = $ChildDomNode->nextSibling;
346 }
347 return $HasTag;
348 }
349 }
350
360 function getObject($node=NULL)
361 {
362 if ($node == NULL)
363 {
364 $node = $this->tree;
365 }
366 if (is_object($node))
367 {
372 $class = $node->class;
373 $obj = new $class;
374 foreach($node->properties as $k=>$v)
375 {
376 $obj->$k = $this->getObject($v);
377 }
378 }
379 elseif (is_array($node))
380 {
381 foreach($node as $k=>$v)
382 {
383 $obj[] = $this->getObject($v);
384 }
385 }
386 else
387 {
388 $obj = $node;
389 }
390 return $obj;
391 }
392
403 function getObjectTree($node=NULL,$prefixClass='')
404 {
405 if ($node == NULL)
406 {
407 $node = $this->tree;
408 }
409 if (is_object($node))
410 {
415 $class = $prefixClass . ($node->class != '' ? $node->class : $node->tag);
416 $obj = new $class;
417 foreach($node->attrs as $k=>$v)
418 {
419 $obj->$k = $v;
420 }
421 foreach($node->properties as $k=>$v)
422 {
423 $obj->$k = $this->getObjectTree($v, $prefixClass);
424 }
425 }
426 elseif (is_array($node))
427 {
428 foreach($node as $k=>$v)
429 {
430 $obj[] = $this->getObjectTree($v, $prefixClass);
431 }
432 }
433 else
434 {
435 $obj = $node;
436 }
437 return $obj;
438 }
439
450 function getInterfaceTree($node=NULL,$prefixClass='')
451 {
452 if ($node == NULL)
453 {
454 $node = $this->tree;
455 }
456 if (is_object($node))
457 {
458 $class = $prefixClass . ($node->class != '' ? $node->class : $node->tag);
459 $obj = new $class;
460 foreach($node->attrs as $k=>$v)
461 {
462 $obj->$k = $v;
463 }
464 foreach($node->properties as $k=>$v)
465 {
466 if (is_object($v))
467 {
468 $obj->{$k}[] = $this->getInterfaceTree($v, $prefixClass);
469 }
470 else
471 {
472 $obj->$k = $this->getInterfaceTree($v, $prefixClass);
473 }
474
475 }
476 }
477 elseif (is_array($node))
478 {
479 foreach($node as $k=>$v)
480 {
481 $obj[] = $this->getInterfaceTree($v, $prefixClass);
482 }
483 }
484 else
485 {
486 $obj = $node;
487 }
488 return $obj;
489 }
490
500 function getXMLTreeElement($node=null)
501 {
502 if (is_object($node))
503 {
504 $obj = new MXMLTreeElement;
505 foreach($node->properties as $k=>$v)
506 {
507 $obj->$k = $this->getXMLTreeElement($v);
508 }
509 }
510 elseif (is_array($node))
511 {
512 foreach($node as $k=>$v)
513 {
514 $obj[] = $this->getXMLTreeElement($v);
515 }
516 }
517 else
518 {
519 $obj = $node;
520 }
521 return $obj;
522 }
523
533 function getXMLTreeAsTreeArray($node=null)
534 {
535 if (is_object($node))
536 {
537 $obj = array();
538 foreach($node->properties as $k=>$v)
539 {
540 $obj[$k] = $this->getXMLTreeAsTreeArray($v);
541 }
542 }
543 elseif (is_array($node))
544 {
545 foreach($node as $k=>$v)
546 {
547 $obj[$k][] = $this->getXMLTreeAsTreeArray($v);
548 }
549 }
550 else
551 {
552 $obj = $node;
553 }
554 return $obj;
555 }
556
568 function getXMLTreeAsFlatArray($node=null, $array=array(), $key='')
569 {
570 if (is_object($node))
571 {
572 foreach($node->properties as $k=>$v)
573 {
574 $kk = $key . ($key != '' ? '.' : '') . $k;
575 $this->getXMLTreeAsFlatArray($v, $array, $kk);
576 }
577 }
578 elseif (is_array($node))
579 {
580 foreach($node as $k=>$v)
581 {
582 $kk = $key . ($key != '' ? '.' : '') . $k;
583 $this->getXMLTreeAsFlatArray($v, $array, $kk);
584 }
585 }
586 else
587 {
588 $array[$key] = $node;
589 }
590 return $array;
591 }
592
601 {
602 $array = array();
603 $xml = $this->getXMLTreeElement($this->tree);
604 $one = true;
605 foreach($xml as $attribute)
606 {
607 if (is_array($attribute) && $one)
608 {
609 $i = 0;
610 foreach($attribute as $element)
611 {
612 $j = 0;
613 foreach($element as $value)
614 {
615 $array[$i][$j++] = $value;
616 }
617 $i++;
618 }
619 }
620 }
621 return $array;
622 }
623
632 {
633 $array = array();
634 $xml = $this->getXMLTreeElement($this->tree);
635 $one = true;
636 foreach($xml as $attribute)
637 {
638 if (is_array($attribute) && $one)
639 {
640 $i = 0;
641 foreach($attribute as $element)
642 {
643 foreach($element as $a=>$value)
644 {
645 $array[$i][$a] = $value;
646 }
647 $i++;
648 }
649 }
650 }
651 return $array;
652 }
653
664 function getXMLTreeAsValueArray($index,$value)
665 {
666 $array = array();
667 $xml = $this->getXMLTreeElement($this->tree);
668 $one = true;
669 foreach($xml as $attribute)
670 {
671 if (is_array($attribute) && $one)
672 {
673 foreach($attribute as $element)
674 {
675 $array[$element->$index] = $element->$value;
676 }
677 }
678 }
679 return $array;
680 }
681}
682?>
getXMLTreeAsAssocArray()
Definição mxmltree.class:631
AddAttribute($attr, $value)
Definição mxmltree.class:198
getInterfaceTree($node=NULL, $prefixClass='')
Definição mxmltree.class:450
_startElement($name, $attrs)
Definição mxmltree.class:239
_characterData($data)
Definição mxmltree.class:305
getXMLTreeAsArray()
Definição mxmltree.class:600
_endElement($name)
Definição mxmltree.class:256
getXMLTreeAsFlatArray($node=null, $array=array(), $key='')
Definição mxmltree.class:568
getXMLTreeElement($node=null)
Definição mxmltree.class:500
Push($node)
Definição mxmltree.class:94
getObject($node=NULL)
Definição mxmltree.class:360
AddPropertyArray($index, $value)
Definição mxmltree.class:173
getXMLTreeAsTreeArray($node=null)
Definição mxmltree.class:533
SetValue($data)
Definição mxmltree.class:212
getObjectTree($node=NULL, $prefixClass='')
Definição mxmltree.class:403
Parse($DomNode)
Definição mxmltree.class:319
getXMLTreeAsValueArray($index, $value)
Definição mxmltree.class:664
AddProperty($index, $value, $key=NULL)
Definição mxmltree.class:146
__construct($file='')
Definição mxmltree.class:74