• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilXML2DOM.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004         +-----------------------------------------------------------------------------+
00005         | ILIAS open source                                                           |
00006         +-----------------------------------------------------------------------------+
00007         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00008         |                                                                             |
00009         | This program is free software; you can redistribute it and/or               |
00010         | modify it under the terms of the GNU General Public License                 |
00011         | as published by the Free Software Foundation; either version 2              |
00012         | of the License, or (at your option) any later version.                      |
00013         |                                                                             |
00014         | This program is distributed in the hope that it will be useful,             |
00015         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00016         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00017         | GNU General Public License for more details.                                |
00018         |                                                                             |
00019         | You should have received a copy of the GNU General Public License           |
00020         | along with this program; if not, write to the Free Software                 |
00021         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00022         +-----------------------------------------------------------------------------+
00023 */
00024 
00025 
00033 class XMLStruct
00034 {
00035         var $childs = array();  // child nodes
00036         var     $parent;                        // parent node
00037         var $name;                              // tag name
00038         var $content = array(); // tag content
00039         var $attrs;                             // tag attributes
00040 
00044         function XMLStruct($a_name = "", $a_attrs = array())
00045         {
00046                 $this->name = $a_name;
00047                 $this->attrs = $a_attrs;
00048         }
00049 
00053         function append($a_name, $a_attrs)
00054         {
00055                 $struct = new XMLStruct($a_name, $a_attrs);
00056                 $struct->parent =& $GLOBALS["lastObj"];
00057 
00058                 $GLOBALS["lastObj"] =& $struct;
00059                 $this->childs[] =& $struct;
00060         }
00061 
00065         function setParent()
00066         {
00067                 $GLOBALS["lastObj"] =& $GLOBALS["lastObj"]->parent;
00068         }
00069 
00073         function setContent($a_data)
00074         {
00075 //echo "<br>XMLStruct:setContent-".$this->name."-$a_data-";
00076                 $this->content[] = $a_data;
00077         }
00078 
00084         function insert(&$dom, &$node)
00085         {
00086                 $newNode = $dom->create_element($this->name);
00087                 if ($this->content != "")
00088                 {
00089                         $newNode->set_content(implode("", $this->content));
00090                 }
00091                 if (is_array($this->attrs))
00092                 {
00093                         #vd($this->attrs);
00094                         reset ($this->attrs);
00095                         while (list ($key, $val) = each ($this->attrs)) {
00096                                 $newNode->set_attribute($key, $val);
00097                         }
00098                 }
00099                 $node = $node->append_child($newNode);
00100                 for ($j = 0; $j < count($this->childs); $j++)
00101                 {
00102                         $this->childs[$j]->insert($dom, $node);
00103                 }
00104                 $node = $node->parent_node();
00105         }
00106 }
00107 
00108 class XML2DOM
00109 {
00110 
00111         function XML2DOM($a_xml)
00112         {
00113                 $xml_parser = xml_parser_create("UTF-8");
00114                 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
00115                 xml_set_object($xml_parser, $this);
00116                 xml_set_element_handler($xml_parser, "startElement", "endElement");
00117                 xml_set_character_data_handler($xml_parser, "characterData");
00118 
00119                 if (!xml_parse($xml_parser, $a_xml, true))
00120                 {
00121                         die(sprintf("XML error: %s at line %d",
00122                             xml_error_string(xml_get_error_code($xml_parser)),
00123                             xml_get_current_line_number($xml_parser)));
00124                 }
00125                 xml_parser_free($xml_parser);
00126         }
00127 
00128         function clean(&$attr)
00129         {
00130                 if(is_array($attr))
00131                 {
00132                         foreach($attr as $key => $value)
00133                         {
00134                                 $attr[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&amp;",$attr[$key]);
00135                                 $attr[$key] = preg_replace("/\"/","&quot;",$attr[$key]);
00136                                 $attr[$key] = preg_replace("/</","&lt;",$attr[$key]);
00137                                 $attr[$key] = preg_replace("/>/","&gt;",$attr[$key]);
00138                         }
00139                 }
00140                 return $attr;
00141         }
00142 
00143 
00144         function startElement($a_parser, $a_name, $a_attrs)
00145         {
00146                 if (!is_object($this->xmlStruct))
00147                 {
00148                         #vd($a_attrs);
00149                         $this->xmlStruct = new XMLStruct($a_name, $a_attrs);
00150                         $GLOBALS["lastObj"] =& $this->xmlStruct;
00151                 }
00152                 else
00153                 {
00154                         $a_attrs = $this->clean($a_attrs);
00155                         #var_dump("<pre>",++$counter," ",$a_name," -> ",$a_attrs,"<pre>");
00156                         $GLOBALS["lastObj"]->append($a_name, $a_attrs);
00157                 }
00158         }
00159 
00160         function endElement($a_parser, $a_name)
00161         {
00162                 $GLOBALS["lastObj"]->setParent();
00163         }
00164 
00165         function characterData($a_parser, $a_data)
00166         {
00167                 $a_data = preg_replace("/&/","&amp;",$a_data);
00168 
00169                 $GLOBALS["lastObj"]->setContent($a_data);
00170         }
00171 
00172         function insertNode(&$dom, &$node)
00173         {
00174                 $node = $this->xmlStruct->insert($dom, $node);
00175         }
00176 }
00177 

Generated on Fri Dec 13 2013 13:52:08 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1