PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-shopping-cart/merchants/library/xml-processing/gc_xmlparser.php

https://github.com/alx/barceloneta
PHP | 202 lines | 97 code | 14 blank | 91 comment | 19 complexity | 3d00ab0e851205cf48c6a1638fbde8a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Classes used to parse xml data
  4. */
  5. /*
  6. Copyright (C) 2007 Google Inc.
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. 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.
  18. For more info: http://code.google.com/p/google-checkout-php-sample-code/
  19. Upgrades (05/23/2007) ropu:
  20. Remove UpdateRecursive()
  21. Support for empty tags (like <world-area/>)
  22. Accept multiple options in a second parameter
  23. *
  24. **/
  25. /* This uses SAX parser to convert XML data into PHP associative arrays
  26. * When invoking the constructor with the input data, strip out the first XML line
  27. *
  28. * Member field Description:
  29. * $params: This stores the XML data. The attributes and contents of XML tags
  30. * can be accessed as follows
  31. *
  32. * <addresses>
  33. * <anonymous-address id="123"> <test>data 1 </test>
  34. * </anonymous-address>
  35. * <anonymous-address id="456"> <test>data 2 </test>
  36. * </anonymous-address>
  37. * </addresses>
  38. *
  39. * print_r($this->params) will return
  40. Array
  41. (
  42. [addresses] => Array
  43. (
  44. [anonymous-address] => Array
  45. (
  46. [0] => Array
  47. (
  48. [id] => 123
  49. [test] => Array
  50. (
  51. [VALUE] => data 1
  52. )
  53. )
  54. [1] => Array
  55. (
  56. [id] => 456
  57. [test] => Array
  58. (
  59. [VALUE] => data 2
  60. )
  61. )
  62. )
  63. )
  64. )
  65. * gc_xmlparser returns an empty params array if it encounters
  66. * any error during parsing
  67. */
  68. // XML to Array
  69. class gc_xmlparser {
  70. var $params = array(); //Stores the object representation of XML data
  71. var $root = NULL;
  72. var $global_index = -1;
  73. var $fold = false;
  74. /* Constructor for the class
  75. * Takes in XML data as input( do not include the <xml> tag
  76. */
  77. function gc_xmlparser($input, $xmlParams=array(XML_OPTION_CASE_FOLDING => 0)) {
  78. $xmlp = xml_parser_create();
  79. foreach($xmlParams as $opt => $optVal) {
  80. switch( $opt ) {
  81. case XML_OPTION_CASE_FOLDING:
  82. $this->fold = $optVal;
  83. break;
  84. default:
  85. break;
  86. }
  87. xml_parser_set_option($xmlp, $opt, $optVal);
  88. }
  89. if(xml_parse_into_struct($xmlp, $input, $vals, $index)) {
  90. $this->root = $this->_foldCase($vals[0]['tag']);
  91. $this->params = $this->xml2ary($vals);
  92. }
  93. xml_parser_free($xmlp);
  94. }
  95. function _foldCase($arg) {
  96. return( $this->fold ? strtoupper($arg) : $arg);
  97. }
  98. /*
  99. * Credits for the structure of this function
  100. * http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html
  101. *
  102. * Adapted by Ropu - 05/23/2007
  103. *
  104. */
  105. function xml2ary($vals) {
  106. $mnary=array();
  107. $ary=&$mnary;
  108. foreach ($vals as $r) {
  109. $t=$r['tag'];
  110. if ($r['type']=='open') {
  111. if (isset($ary[$t]) && !empty($ary[$t])) {
  112. if (isset($ary[$t][0])){
  113. $ary[$t][]=array();
  114. }
  115. else {
  116. $ary[$t]=array($ary[$t], array());
  117. }
  118. $cv=&$ary[$t][count($ary[$t])-1];
  119. }
  120. else {
  121. $cv=&$ary[$t];
  122. }
  123. $cv=array();
  124. if (isset($r['attributes'])) {
  125. foreach ($r['attributes'] as $k=>$v) {
  126. $cv[$k]=$v;
  127. }
  128. }
  129. $cv['_p']=&$ary;
  130. $ary=&$cv;
  131. } else if ($r['type']=='complete') {
  132. if (isset($ary[$t]) && !empty($ary[$t])) { // same as open
  133. if (isset($ary[$t][0])) {
  134. $ary[$t][]=array();
  135. }
  136. else {
  137. $ary[$t]=array($ary[$t], array());
  138. }
  139. $cv=&$ary[$t][count($ary[$t])-1];
  140. }
  141. else {
  142. $cv=&$ary[$t];
  143. }
  144. if (isset($r['attributes'])) {
  145. foreach ($r['attributes'] as $k=>$v) {
  146. $cv[$k]=$v;
  147. }
  148. }
  149. $cv['VALUE'] = (isset($r['value']) ? $r['value'] : '');
  150. } elseif ($r['type']=='close') {
  151. $ary=&$ary['_p'];
  152. }
  153. }
  154. $this->_del_p($mnary);
  155. return $mnary;
  156. }
  157. // _Internal: Remove recursion in result array
  158. function _del_p(&$ary) {
  159. foreach ($ary as $k=>$v) {
  160. if ($k==='_p') {
  161. unset($ary[$k]);
  162. }
  163. else if(is_array($ary[$k])) {
  164. $this->_del_p($ary[$k]);
  165. }
  166. }
  167. }
  168. /* Returns the root of the XML data */
  169. function GetRoot() {
  170. return $this->root;
  171. }
  172. /* Returns the array representing the XML data */
  173. function GetData() {
  174. return $this->params;
  175. }
  176. }
  177. ?>