source: trunk/instant_messenger/inc/class.XML.inc.php @ 20

Revision 20, 3.8 KB checked in by niltonneto, 17 years ago (diff)

Inclusão do módulo Mensageiro Instantâneo no CVS.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2/*
3+------------------------------------------------+
4| @AUTHOR: [NUTS] Rodrigo Souza                  |
5| @DATE: Seg 18 Dez 2006 09:32:04 BRST           |
6| @LAST CHANGE: Qua 20 Dez 2006 16:14:11 BRST::  |
7+------------------------------------------------+
8*/
9
10class XML
11{
12   function __construct()
13   {
14   }
15
16   function __destruct()
17   {
18   }
19
20        final function xmlize($data, $white=1, $encoding='UTF-8')
21   {
22                $data = trim($data);
23                $vals = $index = $array = array();
24                $parser = xml_parser_create($encoding);
25
26                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
27                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $white);
28                //xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
29
30      xml_parse_into_struct($parser, $data, $vals, $index);
31      /*
32      if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
33      {
34         die(sprintf("XML error: %s at line %d",
35            xml_error_string(xml_get_error_code($parser)),
36            xml_get_current_line_number($parser)));
37      }
38      */
39
40                xml_parser_free($parser);
41
42                $i = 0;
43
44                $tagname = $vals[$i]['tag'];
45                if ( isset ($vals[$i]['attributes'] ) )
46                {
47                        $array[$tagname]['@'] = $vals[$i]['attributes'];
48                }
49      else
50      {
51                        $array[$tagname]['@'] = array();
52                }
53
54                $array[$tagname]["#"] = $this->_xml_depth($vals, $i);
55
56                return $array;
57        }
58
59        private final function _xml_depth($vals, &$i)
60   {
61                $children = array();
62
63                if ( isset($vals[$i]['value']) )
64                {
65                        array_push($children, $vals[$i]['value']);
66                }
67
68                while ( ++$i < count($vals) )
69      {
70                        switch ($vals[$i]['type'])
71         {
72                                case 'open':
73                           if ( isset ( $vals[$i]['tag'] ) )
74                           {
75                              $tagname = $vals[$i]['tag'];
76                           }
77                           else
78                           {
79                              $tagname = '';
80                           }
81
82                           if ( isset ( $children[$tagname] ) )
83                           {
84                              $size = sizeof($children[$tagname]);
85                           }
86                           else
87                           {
88                              $size = 0;
89                           }
90
91                           if ( isset ( $vals[$i]['attributes'] ) )
92                           {
93                              $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
94                           }
95
96                           $children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i);
97            // end case 'open'
98                                break;
99                                case 'cdata':
100                           array_push($children, $vals[$i]['value']);
101            // end case 'cdata'
102                                break;
103                                case 'complete':
104                           $tagname = $vals[$i]['tag'];
105
106                           if( isset ($children[$tagname]) )
107                           {
108                              $size = sizeof($children[$tagname]);
109                           }
110                           else
111                           {
112                              $size = 0;
113                           }
114
115                           if( isset ( $vals[$i]['value'] ) )
116                           {
117                              $children[$tagname][$size]["#"] = $vals[$i]['value'];
118                           }
119                           else
120                           {
121                              $children[$tagname][$size]["#"] = array();
122                           }
123
124                           if ( isset ($vals[$i]['attributes']) )
125                           {
126                              $children[$tagname][$size]['@'] = $vals[$i]['attributes'];
127                           }
128            // end case 'complete'
129                                break;
130                                case 'close':
131                           return $children;
132            // end case 'close'
133            break;
134                        }
135                }
136                return $children;
137        }
138}
139?>
Note: See TracBrowser for help on using the repository browser.