ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  public $childs = array(); // child nodes
16  public $parent; // parent node
17  public $name; // tag name
18  public $content = array(); // tag content
19  public $attrs; // tag attributes
20 
24  public function __construct($a_name = "", $a_attrs = array())
25  {
26  $this->name = $a_name;
27  $this->attrs = $a_attrs;
28  }
29 
33  public 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  public function setParent()
46  {
47  $GLOBALS["lastObj"] =&$GLOBALS["lastObj"]->parent;
48  }
49 
53  public function setContent($a_data)
54  {
55  //echo "<br>XMLStruct:setContent-".$this->name."-$a_data-";
56  $this->content[] = $a_data;
57  }
58 
64  public function insert(&$dom, &$node)
65  {
66  $newNode = $dom->create_element($this->name);
67  if ($this->content != "") {
68  $newNode->set_content(implode("", $this->content));
69  }
70  if (is_array($this->attrs)) {
71  #vd($this->attrs);
72  reset($this->attrs);
73  while (list($key, $val) = each($this->attrs)) {
74  $newNode->set_attribute($key, $val);
75  }
76  }
77  $node = $node->append_child($newNode);
78  for ($j = 0; $j < count($this->childs); $j++) {
79  $this->childs[$j]->insert($dom, $node);
80  }
81  $node = $node->parent_node();
82  }
83 }
84 
85 class XML2DOM
86 {
87  public function __construct($a_xml)
88  {
89  $xml_parser = xml_parser_create("UTF-8");
90  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
91  xml_set_object($xml_parser, $this);
92  xml_set_element_handler($xml_parser, "startElement", "endElement");
93  xml_set_character_data_handler($xml_parser, "characterData");
94 
95  if (!xml_parse($xml_parser, $a_xml, true)) {
96  die(sprintf(
97  "XML error: %s at line %d",
98  xml_error_string(xml_get_error_code($xml_parser)),
99  xml_get_current_line_number($xml_parser)
100  ));
101  }
102  xml_parser_free($xml_parser);
103  }
104 
105  public function clean(&$attr)
106  {
107  if (is_array($attr)) {
108  foreach ($attr as $key => $value) {
109  $attr[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/", "&amp;", $attr[$key]);
110  $attr[$key] = preg_replace("/\"/", "&quot;", $attr[$key]);
111  $attr[$key] = preg_replace("/</", "&lt;", $attr[$key]);
112  $attr[$key] = preg_replace("/>/", "&gt;", $attr[$key]);
113  }
114  }
115  return $attr;
116  }
117 
118 
119  public function startElement($a_parser, $a_name, $a_attrs)
120  {
121  if (!is_object($this->xmlStruct)) {
122  #vd($a_attrs);
123  $this->xmlStruct = new XMLStruct($a_name, $a_attrs);
124  $GLOBALS["lastObj"] =&$this->xmlStruct;
125  } else {
126  $a_attrs = $this->clean($a_attrs);
127  #var_dump("<pre>",++$counter," ",$a_name," -> ",$a_attrs,"<pre>");
128  $GLOBALS["lastObj"]->append($a_name, $a_attrs);
129  }
130  }
131 
132  public function endElement($a_parser, $a_name)
133  {
134  $GLOBALS["lastObj"]->setParent();
135  }
136 
137  public function characterData($a_parser, $a_data)
138  {
139  $a_data = preg_replace("/&/", "&amp;", $a_data);
140 
141  $GLOBALS["lastObj"]->setContent($a_data);
142  }
143 
144  public function insertNode(&$dom, &$node)
145  {
146  $node = $this->xmlStruct->insert($dom, $node);
147  }
148 }
insertNode(&$dom, &$node)
__construct($a_name="", $a_attrs=array())
constructor
insert(&$dom, &$node)
insert new node in existing DOM object
clean(&$attr)
__construct($a_xml)
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
endElement($a_parser, $a_name)
startElement($a_parser, $a_name, $a_attrs)
append($a_name, $a_attrs)
append node
Create styles array
The data for the language used.
Class for creating an object (new node) by parsing XML code and adding it to an existing DOM object...
characterData($a_parser, $a_data)
setParent()
set parent node
$key
Definition: croninfo.php:18
setContent($a_data)
set content text