/** * * @get text between tags * @param string $tag The tag name * @param string $html The XML or XHTML string * @param int $strict Whether to use strict mode * @param string $encoding * @return array */ private function getTextBetweenTags($tag, $html, $strict = 0, $encoding = "UTF-8") { global $PAGE, $CFG; if (!isset($CFG->filter_jsxgraph_divid)) { set_config('filter_jsxgraph_divid', 'box'); } if (!isset($CFG->filter_jsxgraph_boardvar)) { set_config('filter_jsxgraph_boardvar', 'board'); } if (!isset($CFG->filter_jsxgraph_width)) { set_config('filter_jsxgraph_width', '500'); } if (!isset($CFG->filter_jsxgraph_height)) { set_config('filter_jsxgraph_height', '400'); } // a new dom object $dom = new domDocument(); $dom->formatOutput = true; // load the html into the object if ($strict == 1) { $dom->loadXML($html); } else { libxml_use_internal_errors(true); $htmlutf8 = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding); $dom->loadHTML($htmlutf8); libxml_use_internal_errors(false); } // discard white space $dom->preserveWhiteSpace = false; $dom->strictErrorChecking = false; $dom->recover = true; // the tag by its tag name $content = $dom->getElementsByTagname($tag); if (count($content) > 0) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/jsxgraphcore.js')); } // Iterate backwards through the jsxgraph tags $i = $content->length - 1; while ($i > -1) { $item = $content->item($i); // Read attributes $w = $item->getAttribute('width'); if ($w == "") { $w = $CFG->filter_jsxgraph_width; } $h = $item->getAttribute('height'); if ($h == "") { $h = $CFG->filter_jsxgraph_height; } $b = $item->getAttribute('box'); if ($b == "") { $b = $CFG->filter_jsxgraph_divid . $i; } $brd = $item->getAttribute('board'); if ($brd == "") { $brd = $CFG->filter_jsxgraph_boardvar . $i; } /* Create new div element containing JSXGraph */ $out = $dom->createElement('div'); $a = $dom->createAttribute('id'); $a->value = $b; $out->appendChild($a); $a = $dom->createAttribute('class'); $a->value = "jxgbox"; $out->appendChild($a); $a = $dom->createAttribute('style'); $a->value = "width:" . $w . "px; height:" . $h . "px; "; $out->appendChild($a); $t = $dom->createTextNode(""); $out->appendChild($t); $out = $dom->appendChild($out); // Replace <jsxgraph> by <div> $item->parentNode->replaceChild($out, $item); $code = ""; $needGXT = false; $url = $item->getAttribute('file'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromFile('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { $url = $item->getAttribute('filestring'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromString('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { // Plain JavaScript code $code = $item->nodeValue; } } // Place JavaScript code at the end of the page. $PAGE->requires->js_init_call($code); if ($needGXT) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/GeonextReader.js')); } --$i; } // remove DOCTYPE $dom->removeChild($dom->firstChild); // remove <html><body></body></html> //$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); //return $dom->saveXML(); $str = $dom->saveHTML(); $str = str_replace("<body>", "", $str); $str = str_replace("</body>", "", $str); $str = str_replace("<html>", "", $str); $str = str_replace("</html>", "", $str); return $str; }
<?php $document = new domDocument("1.0", "utf-8"); $domimp = new domImplementation(); $doctype = $domimp->createDocumentType('asortiment[<!ENTITY y1 "2009"> <!ENTITY y2 "2010">]'); $document->appendChild($doctype); $asortiment = $document->createElementNS("http://mysite.ru", "asort:asortiment"); $document->appendChild($asortiment); $telefon = $document->createElement("asort:telefon"); $asortiment->appendChild($telefon); $attr = $document->createAttribute("number"); $attr->nodeValue = 1; $telefon->appendChild($attr); $cdata = $document->createCDATASection("Это контент нашего интернет магазина"); $telefon->appendChild($cdata); $model_1 = $document->createElement("model"); $telefon->appendChild($model_1); $name_1 = $document->createElement("name"); $model_1->appendChild($name_1); $textN_1 = $document->createTextNode("Samsung Galaxi"); $name_1->appendChild($textN_1); $year_1 = $document->createElement("year"); $model_1->appendChild($year_1); $textY_1 = $document->createTextNode('2009'); $year_1->appendChild($textY_1); $model_2 = $document->createElement("model"); $telefon->appendChild($model_2); $name_2 = $document->createElement("name"); $model_2->appendChild($name_2); $textN_2 = $document->createTextNode("Nokia Lumia"); $name_2->appendChild($textN_2);
***Метод createComment("Comment 1") объекта класса domDocument - создает комментарий (1-значение комментария), так же можно создать комментарий с помощью класса domComment передав конструктору его значение (new domComment("Comment 2")) ***Метод appendChild($comment) - добавляет дочерний элемент в конец(1-указываем элемент который добавляем в XML-документ) ***Метод insertBefore(new domElement("el3"), $element2) - добавляет дочерний элемент перед указанным элементом (1-добавляемый элемент, 2 - элемент перед которым добавляют) ***Метод replaceChild(new domElement("el4"), $element1) - заменяет дочерний элемент на указанный (1-элемент на который заменяем, 2-элемент который заменяем) ***Метод removeChild($element1) - удаляет указанный дочерний элемент(1-элемент который удаляем) ***Метод removeAttributeNode($attr) - удаляет указанный атрибут узла(1-атрибут который хотим удалить) */ $document = new domDocument("1.0", "utf-8"); $element_1 = $document->createElement("element_1", "Text Node"); $element_2 = new domElement("element_2", 145); echo $element_1->nodeName . "<br>"; echo $element_2->nodeName . "<br>"; echo $element_1->nodeValue . "<br/>"; echo $element_2->nodeValue . "<br/>"; echo "---------------------------------------*<br/>"; $attr_1 = $document->createAttribute("attr_1"); $attr_1->nodeValue = 20; $attr_2 = new domAttr("attr_2", 64); echo $attr_1->nodeName . "<br>"; echo $attr_2->nodeName . "<br>"; echo $attr_1->nodeValue . "<br/>"; echo $attr_2->nodeValue . "<br/>"; echo "---------------------------------------*<br/>"; $cdata_1 = $document->createCdataSection("Section CDATA 1"); $cdata_2 = new domCdataSection("Section CDATA 2"); echo $cdata_1->nodeValue . "<br/>"; echo $cdata_2->nodeValue . "<br/>"; echo "---------------------------------------*<br/>"; $text_1 = $document->createTextNode("Text Node 1"); $text_2 = new domText("Text Node 2"); echo $text_1->nodeValue . "<br/>";