Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00035 class ilDOMXML
00036 {
00043 var $doc;
00044
00051 var $tree;
00052
00060 var $error;
00061
00078 function ilDOMXML ()
00079 {
00080 $num = func_num_args();
00081 $args = func_get_args();
00082
00083 if (($num == 1) && is_object($args[0]))
00084 {
00085 $this->doc = $args[0];
00086 }
00087 else
00088 {
00089 $this->initNewDocument($args[0],$args[1],$args[2]);
00090 }
00091 }
00092
00093 function _ilDOMXML ()
00094 {
00095 if (DEBUG)
00096 {
00097 printf("domxml destructor called, class=%s\n", get_class($this)."<br/>");
00098 }
00099 }
00100
00101
00102
00112 function initNewDocument ($a_version = "", $a_encoding = "", $a_charset = "")
00113 {
00114 if (!$a_version) {
00115 $a_version = "1.0";
00116 }
00117
00118 if (!$a_encoding) {
00119 $a_encoding = "UTF-8";
00120 }
00121
00122 if (!$a_charset) {
00123 $a_charset = "UTF-8";
00124 }
00125
00126
00127 $xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
00128 "<root />";
00129
00130
00131 $this->doc = domxml_open_mem($xmlHeader);
00132
00133
00134 $root = $this->doc->document_element();
00135 $root->unlink_node();
00136
00137
00138 $this->setEncoding($a_encoding);
00139 $this->setCharset($a_charset);
00140 }
00141
00151 function createRootElement ($a_element)
00152 {
00153
00154 if ($root = $this->getRoot()) {
00155 return false;
00156 }
00157
00158 return $this->appendChild($this->createElement($a_element));
00159 }
00160
00172 function loadDocument ($a_filename, $a_filepath, $a_validate = false)
00173 {
00174 if ($a_validate) {
00175 $mode = DOMXML_LOAD_VALIDATING;
00176 } else {
00177 $mode = DOMXML_LOAD_PARSING;
00178 }
00179
00180 $this->doc = domxml_open_file($a_filepath . "/" . $a_filename, $mode, $this->error);
00181
00182
00183 if ($this->error) {
00184 $error_msg = "Error(s) while parsing the document!<br><br>";
00185
00186 foreach ($this->error as $error) {
00187 $error_msg .= $error["errormessage"]." in line: ".$error["line"]."<br>";
00188 }
00189
00190
00191 echo $error_msg;
00192 exit();
00193 }
00194
00195
00196 $this->setEncoding("iso-8859-1",true);
00197
00198 $this->setCharset("iso-8859-1");
00199
00200 return $this->doc;
00201 }
00202
00210 function trim ($a_node)
00211 {
00212 if ($a_node->has_child_nodes()) {
00213 $childs = $a_node->child_nodes();
00214
00215 foreach ($childs as $child) {
00216 $content = trim($child->get_content());
00217
00218 if (empty($content)) {
00219 $child->unlink_node();
00220 } else {
00221 $this->trim($child);
00222 }
00223 }
00224 }
00225 }
00226
00235 function trimDocument ($a_node = '')
00236 {
00237 if (empty($a_node)) {
00238 $a_node = $this->doc;
00239 }
00240
00241 $this->trim($a_node);
00242 return $a_node;
00243 }
00244
00266 function transform ($node, $left2 = -1, $lvl = 0)
00267 {
00268 static $left;
00269
00270
00271 $lvl++;
00272
00273
00274 if ($left2 > 0) {
00275 $left = $left2;
00276 }
00277
00278 if (!$left) {
00279 $left = 1;
00280 }
00281
00282 $node2 = (array)$node;
00283
00284
00285
00286
00287
00288
00289 $this->tree[$node2[0]]["struct"] = 0;
00290
00291 if ($parent = $node->parent_node()) {
00292 $parent = (array)$parent;
00293 }
00294
00295 if ($first = $node->first_child())
00296 {
00297 $first = (array)$first;
00298 }
00299
00300 if ($prev = $node->previous_sibling()) {
00301 $prev = (array)$prev;
00302 }
00303
00304 if ($next = $node->next_sibling()) {
00305 $next = (array)$next;
00306 }
00307
00308 $this->tree[$node2[0]]["content"] = trim($node->node_value());
00309 $this->tree[$node2[0]]["name"] = $node->node_name();
00310 $this->tree[$node2[0]]["type"] = $node->type;
00311 $this->tree[$node2[0]]["depth"] = $lvl;
00312 $this->tree[$node2[0]]["parent"] = $parent[0];
00313 $this->tree[$node2[0]]["first"] = $first[0];
00314 $this->tree[$node2[0]]["prev"] = $prev[0];
00315 $this->tree[$node2[0]]["next"] = $next[0];
00316 $this->tree[$node2[0]]["left"] = $left;
00317 $left++;
00318
00319
00320 if ($node->has_attributes())
00321 {
00322 $data = "";
00323
00324 foreach ($node->attributes() as $attribute)
00325 {
00326 $data[$attribute->name] = $attribute->value;
00327 }
00328
00329 $this->tree[$node2[0]]["attr_list"] = $data;
00330 $this->tree[$node2[0]]["struct"] += 1;
00331 }
00332
00333
00334 foreach ($node->child_nodes() as $child)
00335 {
00336 if ($child->node_type() == XML_TEXT_NODE)
00337 {
00338 $this->tree[$node2[0]]["struct"] += 2;
00339 break;
00340 }
00341 }
00342
00343
00344
00345 foreach ($node->child_nodes() as $child)
00346 {
00347 $this->transform($child, $left, $lvl);
00348 }
00349
00350 $this->tree[$node2[0]]["right"] = $left;
00351 $left++;
00352 }
00353
00364 function buildTree ($a_node = "")
00365 {
00366 if (empty($a_node)) {
00367 $a_node = $this->doc;
00368 }
00369
00370 $this->transform($a_node,1);
00371
00372 return $this->tree;
00373 }
00374
00387 function dumpDocument ($a_stdout = -1, $a_compress = false, $a_format = false)
00388 {
00389 if ($a_stdout != -1) {
00390 $this->doc->dump_file($a_stdout,$a_compress,$a_format);
00391 }
00392
00393 return $this->doc->dump_mem();
00394 }
00395
00408 function getTextFromElement ($a_element)
00409 {
00410 if ($a_element->node_type() == XML_ELEMENT_NODE) {
00411 $value = "";
00412
00413 foreach ($a_element->child_nodes() as $child) {
00414 if ($child->node_type() == XML_TEXT_NODE) {
00415 $value .= $child->content;
00416 }
00417 }
00418
00419 return trim($value);
00420 }
00421
00422 die("<b>".$a_element."</b> is not a valid element node!");
00423 }
00424
00435 function isLeafElement ($a_node, $a_elementname, $a_num = 0)
00436 {
00437 $var = true;
00438
00439 if ($childs = $a_node->child_nodes()) {
00440 foreach ($childs as $child) {
00441 $var = $this->isLeafElement($child, $a_elementname);
00442
00443 if (!$var) {
00444 return false;
00445 }
00446 }
00447 }
00448
00449 if (($a_node->node_type() == XML_ELEMENT_NODE) && ($a_node->tagname == $a_elementname) && ($a_num != 1)) {
00450 return false;
00451 }
00452
00453 return $var;
00454 }
00455
00467 function getElementsByTagname ($a_elementname, $a_node = "")
00468 {
00469 if (empty($a_node)) {
00470 $a_node = $this->doc;
00471 }
00472
00473 if (count($node = $a_node->get_elements_by_tagname($a_elementname)) > 0) {
00474 return $node;
00475 }
00476
00477 return false;
00478 }
00479
00490 function appendReferenceNodeForLO ($a_node, $a_lo_id, $a_lm_id, $a_prev_sibling)
00491 {
00492 $newnode = $this->createElement("LO");
00493
00494 if (empty($a_prev_sibling))
00495 {
00496 $node = $a_node->append_child($newnode);
00497 }
00498 else
00499 {
00500 $node = $a_prev_sibling->append_sibling($newnode);
00501 }
00502
00503 $node->set_attribute("id",$a_lo_id);
00504 $node->set_attribute("lm",$a_lm_id);
00505 }
00506
00515 function appendChild ($a_node)
00516 {
00517 return $this->doc->append_child($a_node);
00518 }
00519
00527 function getRoot ()
00528 {
00529 return $this->doc->document_element();
00530 }
00531
00540 function createElement ($a_node)
00541 {
00542 return $this->doc->create_element($a_node);
00543 }
00544
00545 function addElement ($a_parent, $a_node)
00546 {
00547 $node = $this->doc->create_element($a_node);
00548 $node = $a_parent->append_child($node);
00549
00550 return $node;
00551 }
00552
00561 function createText ($a_text)
00562 {
00563 return $this->doc->create_text_node($a_text);
00564 }
00565
00578 function createNode ($a_parent, $a_elementname, $a_attr_list = NULL, $a_text = NULL)
00579 {
00580
00581 $node = $this->createElement($a_elementname);
00582
00583
00584 if (is_array($a_attr_list)) {
00585 foreach ($a_attr_list as $attr => $value) {
00586 $node->set_attribute($attr, $value);
00587 }
00588 }
00589
00590
00591 if (is_string($a_text)) {
00592 $node_text = $this->doc->create_text_node($a_text);
00593 $node_text = $node->append_child($node_text);
00594 }
00595
00596
00597 $node = $a_parent->append_child($node);
00598
00599 return $node;
00600 }
00601
00609 function getElementId($a_node)
00610 {
00611 $node = (array) $a_node;
00612 return $node[0];
00613 }
00614
00622 function getElementName($a_node)
00623 {
00624 return $a_node->node_name();
00625 }
00626
00635 function setEncoding ($a_encode,$a_overwrite = false)
00636 {
00637 if (empty($this->doc->encoding) or ($a_overwrite)) {
00638 $this->doc->encoding = $a_encode;
00639 return true;
00640 }
00641
00642 return false;
00643 }
00644
00651 function getEncoding ()
00652 {
00653 return $this->doc->encoding;
00654 }
00655
00664 function setCharset ($a_charset,$a_overwrite = false)
00665 {
00666 if (is_integer($this->doc->charset) or ($a_overwrite)) {
00667 $this->doc->charset = $a_charset;
00668 return true;
00669 }
00670
00671 return false;
00672 }
00673
00680 function getCharset ()
00681 {
00682 return $this->doc->charset;
00683 }
00684
00691 function getInfo ()
00692 {
00693 $node = $this->getElementsByTagname("MetaData");
00694
00695 if($node !== false)
00696 {
00697 $childs = $node[0]->child_nodes();
00698
00699 foreach ($childs as $child)
00700 {
00701 if (($child->node_type() == XML_ELEMENT_NODE) && ($child->tagname == "General"))
00702 {
00703 $childs2 = $child->child_nodes();
00704
00705 foreach ($childs2 as $child2)
00706 {
00707 if (($child2->node_type() == XML_ELEMENT_NODE) && ($child2->tagname == "Title" || $child2->tagname == "Description"))
00708 {
00709 $arr[$child2->tagname] = $child2->get_content();
00710 }
00711 }
00712
00713
00714 break;
00715 }
00716 }
00717 }
00718
00719
00720 $arr["title"] = $arr["Title"];
00721 $arr["desc"] = $arr["Description"];
00722
00723 return $arr;
00724 }
00725
00732 function getReferences()
00733 {
00734 if ($nodes = $this->getElementsByTagname("LO"))
00735 {
00736 foreach ($nodes as $node)
00737 {
00738 $attr[] = $node->get_attribute("id");
00739 }
00740 }
00741
00742 return $attr;
00743 }
00744 }
00745 ?>