ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilXML2DOM.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
13 class XMLStruct
14 {
15  var $childs = array(); // child nodes
16  var $parent; // parent node
17  var $name; // tag name
18  var $content = array(); // tag content
19  var $attrs; // tag attributes
20 
24  function XMLStruct($a_name = "", $a_attrs = array())
25  {
26  $this->name = $a_name;
27  $this->attrs = $a_attrs;
28  }
29 
33  function append($a_name, $a_attrs)
34  {
35  $struct = new XMLStruct($a_name, $a_attrs);
36  $struct->parent =& $GLOBALS["lastObj"];
37 
38  $GLOBALS["lastObj"] =& $struct;
39  $this->childs[] =& $struct;
40  }
41 
45  function setParent()
46  {
47  $GLOBALS["lastObj"] =& $GLOBALS["lastObj"]->parent;
48  }
49 
53  function setContent($a_data)
54  {
55 //echo "<br>XMLStruct:setContent-".$this->name."-$a_data-";
56  $this->content[] = $a_data;
57  }
58 
64  function insert(&$dom, &$node)
65  {
66  $newNode = $dom->create_element($this->name);
67  if ($this->content != "")
68  {
69  $newNode->set_content(implode("", $this->content));
70  }
71  if (is_array($this->attrs))
72  {
73  #vd($this->attrs);
74  reset ($this->attrs);
75  while (list ($key, $val) = each ($this->attrs)) {
76  $newNode->set_attribute($key, $val);
77  }
78  }
79  $node = $node->append_child($newNode);
80  for ($j = 0; $j < count($this->childs); $j++)
81  {
82  $this->childs[$j]->insert($dom, $node);
83  }
84  $node = $node->parent_node();
85  }
86 }
87 
88 class XML2DOM
89 {
90 
91  function XML2DOM($a_xml)
92  {
93  $xml_parser = xml_parser_create("UTF-8");
94  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
95  xml_set_object($xml_parser, $this);
96  xml_set_element_handler($xml_parser, "startElement", "endElement");
97  xml_set_character_data_handler($xml_parser, "characterData");
98 
99  if (!xml_parse($xml_parser, $a_xml, true))
100  {
101  die(sprintf("XML error: %s at line %d",
102  xml_error_string(xml_get_error_code($xml_parser)),
103  xml_get_current_line_number($xml_parser)));
104  }
105  xml_parser_free($xml_parser);
106  }
107 
108  function clean(&$attr)
109  {
110  if(is_array($attr))
111  {
112  foreach($attr as $key => $value)
113  {
114  $attr[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&amp;",$attr[$key]);
115  $attr[$key] = preg_replace("/\"/","&quot;",$attr[$key]);
116  $attr[$key] = preg_replace("/</","&lt;",$attr[$key]);
117  $attr[$key] = preg_replace("/>/","&gt;",$attr[$key]);
118  }
119  }
120  return $attr;
121  }
122 
123 
124  function startElement($a_parser, $a_name, $a_attrs)
125  {
126  if (!is_object($this->xmlStruct))
127  {
128  #vd($a_attrs);
129  $this->xmlStruct = new XMLStruct($a_name, $a_attrs);
130  $GLOBALS["lastObj"] =& $this->xmlStruct;
131  }
132  else
133  {
134  $a_attrs = $this->clean($a_attrs);
135  #var_dump("<pre>",++$counter," ",$a_name," -> ",$a_attrs,"<pre>");
136  $GLOBALS["lastObj"]->append($a_name, $a_attrs);
137  }
138  }
139 
140  function endElement($a_parser, $a_name)
141  {
142  $GLOBALS["lastObj"]->setParent();
143  }
144 
145  function characterData($a_parser, $a_data)
146  {
147  $a_data = preg_replace("/&/","&amp;",$a_data);
148 
149  $GLOBALS["lastObj"]->setContent($a_data);
150  }
151 
152  function insertNode(&$dom, &$node)
153  {
154  $node = $this->xmlStruct->insert($dom, $node);
155  }
156 }
157