ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilXML2DOM.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 
25 
33 class XMLStruct
34 {
35  var $childs = array(); // child nodes
36  var $parent; // parent node
37  var $name; // tag name
38  var $content = array(); // tag content
39  var $attrs; // tag attributes
40 
44  function XMLStruct($a_name = "", $a_attrs = array())
45  {
46  $this->name = $a_name;
47  $this->attrs = $a_attrs;
48  }
49 
53  function append($a_name, $a_attrs)
54  {
55  $struct = new XMLStruct($a_name, $a_attrs);
56  $struct->parent =& $GLOBALS["lastObj"];
57 
58  $GLOBALS["lastObj"] =& $struct;
59  $this->childs[] =& $struct;
60  }
61 
65  function setParent()
66  {
67  $GLOBALS["lastObj"] =& $GLOBALS["lastObj"]->parent;
68  }
69 
73  function setContent($a_data)
74  {
75 //echo "<br>XMLStruct:setContent-".$this->name."-$a_data-";
76  $this->content[] = $a_data;
77  }
78 
84  function insert(&$dom, &$node)
85  {
86  $newNode = $dom->create_element($this->name);
87  if ($this->content != "")
88  {
89  $newNode->set_content(implode("", $this->content));
90  }
91  if (is_array($this->attrs))
92  {
93  #vd($this->attrs);
94  reset ($this->attrs);
95  while (list ($key, $val) = each ($this->attrs)) {
96  $newNode->set_attribute($key, $val);
97  }
98  }
99  $node = $node->append_child($newNode);
100  for ($j = 0; $j < count($this->childs); $j++)
101  {
102  $this->childs[$j]->insert($dom, $node);
103  }
104  $node = $node->parent_node();
105  }
106 }
107 
108 class XML2DOM
109 {
110 
111  function XML2DOM($a_xml)
112  {
113  $xml_parser = xml_parser_create("UTF-8");
114  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
115  xml_set_object($xml_parser, $this);
116  xml_set_element_handler($xml_parser, "startElement", "endElement");
117  xml_set_character_data_handler($xml_parser, "characterData");
118 
119  if (!xml_parse($xml_parser, $a_xml, true))
120  {
121  die(sprintf("XML error: %s at line %d",
122  xml_error_string(xml_get_error_code($xml_parser)),
123  xml_get_current_line_number($xml_parser)));
124  }
125  xml_parser_free($xml_parser);
126  }
127 
128  function clean(&$attr)
129  {
130  if(is_array($attr))
131  {
132  foreach($attr as $key => $value)
133  {
134  $attr[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&amp;",$attr[$key]);
135  $attr[$key] = preg_replace("/\"/","&quot;",$attr[$key]);
136  $attr[$key] = preg_replace("/</","&lt;",$attr[$key]);
137  $attr[$key] = preg_replace("/>/","&gt;",$attr[$key]);
138  }
139  }
140  return $attr;
141  }
142 
143 
144  function startElement($a_parser, $a_name, $a_attrs)
145  {
146  if (!is_object($this->xmlStruct))
147  {
148  #vd($a_attrs);
149  $this->xmlStruct = new XMLStruct($a_name, $a_attrs);
150  $GLOBALS["lastObj"] =& $this->xmlStruct;
151  }
152  else
153  {
154  $a_attrs = $this->clean($a_attrs);
155  #var_dump("<pre>",++$counter," ",$a_name," -> ",$a_attrs,"<pre>");
156  $GLOBALS["lastObj"]->append($a_name, $a_attrs);
157  }
158  }
159 
160  function endElement($a_parser, $a_name)
161  {
162  $GLOBALS["lastObj"]->setParent();
163  }
164 
165  function characterData($a_parser, $a_data)
166  {
167  $a_data = preg_replace("/&/","&amp;",$a_data);
168 
169  $GLOBALS["lastObj"]->setContent($a_data);
170  }
171 
172  function insertNode(&$dom, &$node)
173  {
174  $node = $this->xmlStruct->insert($dom, $node);
175  }
176 }
177