PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/OX/Workflow/XMLToArray.php

https://github.com/orchestra-io/sample-openx
PHP | 128 lines | 44 code | 20 blank | 64 comment | 1 complexity | 18746a23666e811a812982722c67bf5b MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v${RELEASE_MAJOR_MINOR} |
  5. | =======${RELEASE_MAJOR_MINOR_DOUBLE_UNDERLINE} |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id:$
  25. */
  26. /**
  27. * A custom XML parser facility from
  28. * http://www.devarticles.com/c/a/PHP/Converting-XML-Into-a-PHP-Data-Structure/
  29. */
  30. class OX_Workflow_XMLToArray
  31. {
  32. //-----------------------------------------
  33. // private variables
  34. var $parser;
  35. var $node_stack = array();
  36. //-----------------------------------------
  37. /** PUBLIC
  38. * If a string is passed in, parse it right away.
  39. */
  40. function XMLToArray($xmlstring="") {
  41. if ($xmlstring) return($this->parse($xmlstring));
  42. return(true);
  43. }
  44. //-----------------------------------------
  45. /** PUBLIC
  46. * Parse a text string containing valid XML into a multidimensional array
  47. * located at rootnode.
  48. */
  49. function parse($xmlstring="") {
  50. // set up a new XML parser to do all the work for us
  51. $this->parser = xml_parser_create();
  52. xml_set_object($this->parser, $this);
  53. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  54. xml_set_element_handler($this->parser, "startElement", "endElement");
  55. xml_set_character_data_handler($this->parser, "characterData");
  56. // Build a Root node and initialize the node_stack...
  57. $this->node_stack = array();
  58. $this->startElement(null, "root", array());
  59. // parse the data and free the parser...
  60. xml_parse($this->parser, $xmlstring);
  61. xml_parser_free($this->parser);
  62. // recover the root node from the node stack
  63. $rnode = array_pop($this->node_stack);
  64. // return the root node...
  65. return($rnode);
  66. }
  67. //-----------------------------------------
  68. /** PROTECTED
  69. * Start a new Element. This means we push the new element onto the stack
  70. * and reset it's properties.
  71. */
  72. function startElement($parser, $name, $attrs)
  73. {
  74. // create a new node...
  75. $node = array();
  76. $node["_NAME"] = $name;
  77. foreach ($attrs as $key => $value) {
  78. $node[$key] = $value;
  79. }
  80. $node["_DATA"] = "";
  81. $node["_ELEMENTS"] = array();
  82. // add the new node to the end of the node stack
  83. array_push($this->node_stack, $node);
  84. }
  85. //-----------------------------------------
  86. /** PROTECTED
  87. * End an element. This is done by popping the last element from the
  88. * stack and adding it to the previous element on the stack.
  89. */
  90. function endElement($parser, $name) {
  91. // pop this element off the node stack
  92. $node = array_pop($this->node_stack);
  93. $node["_DATA"] = trim($node["_DATA"]);
  94. // and add it an an element of the last node in the stack...
  95. $lastnode = count($this->node_stack);
  96. array_push($this->node_stack[$lastnode-1]["_ELEMENTS"], $node);
  97. }
  98. //-----------------------------------------
  99. /** PROTECTED
  100. * Collect the data onto the end of the current chars.
  101. */
  102. function characterData($parser, $data) {
  103. // add this data to the last node in the stack...
  104. $lastnode = count($this->node_stack);
  105. $this->node_stack[$lastnode-1]["_DATA"] .= $data;
  106. }
  107. //-----------------------------------------
  108. }