+
+ public static function parseString($s, $strict = true)
+ {
+ // the "strict" parameter is deactivated
+ libxml_use_internal_errors(true);
+
+ $x = @simplexml_load_string($s);
+ if (!$x) {
+ Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
+ foreach (libxml_get_errors() as $err) {
+ Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ":" . $err->column, 'message' => $err->message]);
+ }
+ Logger::debug('Erroring XML string', ['xml' => $s]);
+ libxml_clear_errors();
+ }
+ return $x;
+ }
+
+ public static function getFirstNodeValue(DOMXPath $xpath, $element, $context = null)
+ {
+ $result = $xpath->evaluate($element, $context);
+ if (!is_object($result)) {
+ return '';
+ }
+
+ $first_item = $result->item(0);
+ if (!is_object($first_item)) {
+ return '';
+ }
+
+ return $first_item->nodeValue;
+ }
+
+ public static function getFirstAttributes(DOMXPath $xpath, $element, $context = null)
+ {
+ $result = $xpath->query($element, $context);
+ if (!is_object($result)) {
+ return false;
+ }
+
+ $first_item = $result->item(0);
+ if (!is_object($first_item)) {
+ return false;
+ }
+
+ return $first_item->attributes;
+ }
+
+ /**
+ * escape text ($str) for XML transport
+ *
+ * @param string $str
+ * @return string Escaped text.
+ */
+ public static function escape($str)
+ {
+ $buffer = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
+ $buffer = trim($buffer);
+
+ return $buffer;
+ }
+
+ /**
+ * undo an escape
+ *
+ * @param string $s xml escaped text
+ * @return string unescaped text
+ */
+ public static function unescape($s)
+ {
+ $ret = htmlspecialchars_decode($s, ENT_QUOTES);
+ return $ret;
+ }
+
+ /**
+ * apply escape() to all values of array $val, recursively
+ *
+ * @param array $val
+ * @return array|string
+ */
+ public static function arrayEscape($val)
+ {
+ if (is_bool($val)) {
+ return $val ? 'true' : 'false';
+ } elseif (is_array($val)) {
+ return array_map('XML::arrayEscape', $val);
+ }
+
+ return self::escape((string) $val);
+ }