X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FXML.php;h=e8eb4233b1a8a3040da04da6b68cb02c7aaadc50;hb=720a43461d67ab229de0aecfc5008f22cc4c1c54;hp=476ecb4b00e7f964325e647472afd8b6bcf345c6;hpb=c1f99c70b1c7d62120723f3b142e843ba25ab338;p=friendica.git diff --git a/src/Util/XML.php b/src/Util/XML.php index 476ecb4b00..e8eb4233b1 100644 --- a/src/Util/XML.php +++ b/src/Util/XML.php @@ -1,7 +1,24 @@ . + * */ + namespace Friendica\Util; use DOMXPath; @@ -10,12 +27,12 @@ use Friendica\Core\System; use SimpleXMLElement; /** - * @brief This class contain methods to work with XML data + * This class contain methods to work with XML data */ class XML { /** - * @brief Creates an XML structure out of a given array + * Creates an XML structure out of a given array * * @param array $array The array of the XML structure that will be generated * @param object $xml The createdXML will be returned by reference @@ -114,7 +131,7 @@ class XML } /** - * @brief Copies an XML object + * Copies an XML object * * @param object $source The XML source * @param object $target The XML target @@ -134,7 +151,7 @@ class XML } /** - * @brief Create an XML element + * Create an XML element * * @param \DOMDocument $doc XML root * @param string $element XML element name @@ -156,7 +173,7 @@ class XML } /** - * @brief Create an XML and append it to the parent object + * Create an XML and append it to the parent object * * @param \DOMDocument $doc XML root * @param object $parent parent object @@ -172,7 +189,7 @@ class XML } /** - * @brief Convert an XML document to a normalised, case-corrected array + * Convert an XML document to a normalised, case-corrected array * used by webfinger * * @param object $xml_element The XML document @@ -223,7 +240,7 @@ class XML } /** - * @brief Convert the given XML text to an array in the XML structure. + * Convert the given XML text to an array in the XML structure. * * Xml::toArray() will convert the given XML text to an array in the XML structure. * Link: http://www.bin-co.com/php/scripts/xml2array/ @@ -251,7 +268,7 @@ class XML } if (!function_exists('xml_parser_create')) { - Logger::log('Xml::toArray: parser function missing'); + Logger::notice('Xml::toArray: parser function missing'); return []; } @@ -266,7 +283,7 @@ class XML } if (! $parser) { - Logger::log('Xml::toArray: xml_parser_create: no resource'); + Logger::notice('Xml::toArray: xml_parser_create: no resource'); return []; } @@ -278,9 +295,9 @@ class XML @xml_parser_free($parser); if (! $xml_values) { - Logger::log('Xml::toArray: libxml: parse error: ' . $contents, Logger::DATA); + Logger::debug('Xml::toArray: libxml: parse error: ' . $contents); foreach (libxml_get_errors() as $err) { - Logger::log('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, Logger::DATA); + Logger::debug('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message); } libxml_clear_errors(); return; @@ -401,7 +418,7 @@ class XML } /** - * @brief Delete a node in a XML object + * Delete a node in a XML object * * @param \DOMDocument $doc XML document * @param string $node Node name @@ -416,18 +433,26 @@ class XML } } - public static function parseString($s, $strict = true) + /** + * Parse XML string + * + * @param string $s + * @param boolean $suppress_log + * @return Object + */ + public static function parseString(string $s, bool $suppress_log = false) { - // 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]); + if (!$suppress_log) { + 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]); } - Logger::debug('Erroring XML string', ['xml' => $s]); libxml_clear_errors(); } return $x; @@ -463,6 +488,21 @@ class XML return $first_item->attributes; } + public static function getFirstValue($xpath, $search, $context) + { + $result = $xpath->query($search, $context); + if (!is_object($result)) { + return ''; + } + + $first_item = $result->item(0); + if (!is_object($first_item)) { + return ''; + } + + return $first_item->nodeValue; + } + /** * escape text ($str) for XML transport *