27 protected static function xmlError(
int $errno,
string $errstr, ?
string $errfile =
null, ?
int $errline =
null, ?array $errcontext =
null,
bool $ret =
false)
29 static $errs = array();
31 $tag =
'DOMDocument::validate(): ';
32 $errs[] = str_replace($tag,
'', $errstr);
39 public function docFromString(
string $xml, ?
string &$error_str): ?\DOMDocument
41 $doc = new \DOMDocument();
42 set_error_handler(
'ILIAS\COPage\Dom\DomUtil::xmlError');
43 $old = ini_set(
'html_errors',
false);
44 $success = $doc->loadXML($xml);
46 ini_set(
'html_errors', $old);
47 restore_error_handler();
51 foreach ($error_arr as
$error) {
52 $error = str_replace(
"DOMDocument::loadXML():",
"",
$error);
53 $error_str .=
$error .
"<br />";
60 public function validate(\DOMDocument $doc, ?
string &
$error, $throw =
false): void
62 $ok = $doc->validate();
65 $error = array(array(
"0",
"Unknown Error"));
67 if (function_exists(
"libxml_get_last_error")) {
68 $err = libxml_get_last_error();
70 if (is_object($err)) {
72 throw new \ilCOPageException($err->message);
74 $error = array(array($err->code, $err->message));
80 public function path(\DOMDocument $doc,
string $path): \DOMNodeList
82 $xpath = new \DOMXPath($doc);
83 return $xpath->query(
$path);
90 bool $keep_attributes =
true
92 $newnode = $node->ownerDocument->createElement($name);
94 foreach ($node->childNodes as $child) {
95 $child2 = $child->cloneNode(
true);
96 $newnode->appendChild($child2);
98 if ($keep_attributes) {
99 foreach ($node->attributes as $attrName => $attrNode) {
100 $newnode->setAttribute($attrName, $attrNode);
103 $node->parentNode->replaceChild($newnode, $node);
113 $newnode = $node->ownerDocument->createElement($name);
114 $par = $node->parentNode;
115 if ($next_sib = $node->nextSibling) {
116 $newnode = $par->insertBefore($newnode, $next_sib);
118 $newnode = $par->appendChild($newnode);
121 $node = $par->removeChild($node);
122 $newnode->appendChild($node);
129 foreach ($node->childNodes as $child) {
130 $child2 = $child->cloneNode(
true);
131 $node->parentNode->insertBefore($child2, $node);
133 $node->parentNode->removeChild($node);
141 foreach ($parent->childNodes as $child) {
142 if (in_array($child->nodeName, $node_names)) {
143 $parent->removeChild($child);
151 while ($parent->hasChildNodes()) {
152 $parent->removeChild($parent->firstChild);
163 foreach ($attributes as $attribute => $value) {
164 $this->setAttribute($node, $attribute, $value);
173 if (!is_null($node)) {
174 if (!is_null($value) && $value !==
"") {
175 $node->setAttribute($attribute, $value);
176 } elseif ($node->hasAttribute($attribute)) {
177 $node->removeAttribute($attribute);
189 \DOMNode $parent_node,
194 bool $remove_childs =
true
196 $doc = $parent_node->ownerDocument;
197 $search = $successors;
198 $search[] = $node_name;
203 foreach ($parent_node->childNodes as $child) {
204 $child_name = $child->nodeName;
205 if (in_array($child_name, $search)) {
212 $new_node = $doc->createElement($node_name);
213 $new_node = $parent_node->appendChild($new_node);
214 if ($content !=
"") {
215 $this->setContent($new_node, $content);
217 $this->setAttributes($new_node, $attributes);
219 if ($child_name == $node_name) {
220 if ($remove_childs) {
221 foreach ($child->childNodes as $child2) {
222 $child->removeChild($child2);
225 if ($content !=
"") {
226 $this->setContent($child, $content);
228 $this->setAttributes($child, $attributes);
230 $new_node = $doc->createElement($node_name);
231 $new_node = $child->parentNode->insertBefore($new_node, $child);
232 if ($content !=
"") {
233 $this->setContent($new_node, $content);
235 $this->setAttributes($new_node, $attributes);
240 public function dump(\DOMNode $node): string
242 return $node->ownerDocument->saveXML($node);
245 public function setContent(\DOMNode $node,
string $text): void
250 $text = str_replace(
"<",
"<", $text);
251 $text = str_replace(
">",
">", $text);
252 $text = str_replace(
"&",
"&", $text);
254 $text_node = new \DOMText();
255 $text_node->appendData($text);
256 if (is_object($node->firstChild)) {
257 $node->replaceChild($text_node, $node->firstChild);
259 $node->appendChild($text_node);
265 $text_node = $node->firstChild;
267 if (is_object($text_node)) {
268 return $text_node->textContent;
280 \DOMNode $parent_node,
286 $doc = $parent_node->ownerDocument;
287 $search = $a_successors;
289 $childs = $parent_node->childNodes;
291 foreach ($childs as $child) {
292 $child_name = $child->nodeName;
293 if (in_array($child_name, $search)) {
299 $new_node = $doc->createElement($a_node_name);
301 $new_node = $parent_node->appendChild($new_node);
303 $new_node = $parent_node->insertBefore($new_node, $child);
305 if ($a_content !=
"") {
306 $this->setContent($new_node, $a_content);
308 $this->setAttributes($new_node, $a_attributes);
replaceByChilds(\DOMNode $node)
addParent(\DOMNode $node, string $name)
validate(\DOMDocument $doc, ?string &$error, $throw=false)
setFirstOptionalElement(\DOMNode $parent_node, string $node_name, array $successors, string $content, array $attributes, bool $remove_childs=true)
searches for an element $node_name within the childs of $parent_node if no node is found,...
setAttribute(?\DOMNode $node, string $attribute, ?string $value)
getContent(\DOMNode $node)
deleteAllChilds(\DOMNode $parent)
docFromString(string $xml, ?string &$error_str)
changeName(\DOMNode $node, string $name, bool $keep_attributes=true)
deleteAllChildsByName(\DOMNode $parent, array $node_names)
setContent(\DOMNode $node, string $text)
static xmlError(int $errno, string $errstr, ?string $errfile=null, ?int $errline=null, ?array $errcontext=null, bool $ret=false)
setAttributes(?\DOMNode $node, array $attributes)
set attributes of a node
path(\DOMDocument $doc, string $path)
addElementToList(\DOMNode $parent_node, string $a_node_name, array $a_successors, string $a_content, array $a_attributes)
Places a new node $a_node_name directly before nodes with names of $a_successors.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))