@param string $xml

@return XmlException|null

@throws \InvalidArgumentException
420000
private function getError(string $xml)
    {
        if ([] !== ($errors = libxml_get_errors())) {
            libxml_clear_errors();
            return new XmlException($errors, sprintf("Invalid xml string \n\n%s\n", $xml));
        }
        return null;
    }
281571
public static function arrayFromXml($xml)
    {
        $document = new DOMDocument('1.0', 'UTF-8');
        $document->loadXML($xml);

        $root = $document->documentElement->nodeName;

        return Util::delimiterToCamelCaseArray([
            $root => self::_nodeToValue($document->childNodes->item(0)),
        ]);
    }
333966
public static function xmlToArray($xml) {
	  // check passed data
    if(!is_string($xml)) {
	    // bad data possed
      throw new LinkedInException('LinkedIn->xmlToArray(): bad data passed, $xml must be a non-zero length string.');
	  }
	  
	  $parser = xml_parser_create();
	  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    if(xml_parse_into_struct($parser, $xml, $tags)) {
	    $elements = array();
      $stack    = array();
      foreach($tags as $tag) {
        $index = count($elements);
        if($tag['type'] == 'complete' || $tag['type'] == 'open') {
          $elements[$tag['tag']]               = array();
          $elements[$tag['tag']]['attributes'] = (array_key_exists('attributes', $tag)) ? $tag['attributes'] : NULL;
          $elements[$tag['tag']]['content']    = (array_key_exists('value', $tag)) ? $tag['value'] : NULL;
          if($tag['type'] == 'open') {
            $elements[$tag['tag']]['children'] = array();
            $stack[count($stack)] = &$elements;
            $elements = &$elements[$tag['tag']]['children'];
          }
        }
        if($tag['type'] == 'close') {
          $elements = &$stack[count($stack) - 1];
          unset($stack[count($stack) - 1]);
        }
      }
      $return_data = $elements;
	  } else {
	    // not valid xml data
	    $return_data = FALSE;
	  }
	  xml_parser_free($parser);
    return $return_data;
  }
891502
public static function fromXml(\DOMNodeList $xml)
    {
        $value = null;
        if ($xml->length > 0) {
            $value = new \DateTimeImmutable($xml->item(0)->textContent);
        }
        return new static($value);
    }
310455
protected function sanitizeXml($xml)
    {
        // Sanitize the XML if requested:
        $newXML = trim(preg_replace($this->sanitizeRegex, ' ', $xml, -1, $count));

        if ($count > 0 && $this->badXmlLog) {
            $this->logBadXML($xml);
        }

        return $newXML;
    }
934499
public function createResponseFromXml($xml)
    {
        $dom = new \DomDocument($xml);
        if (!$dom->loadXML($xml)) {
            return;
        }

        $data = array();

        foreach($dom->childNodes as $root) {
            if ($root->nodeType !== XML_ELEMENT_NODE) {
                continue;
            }
            foreach($root->childNodes as $node) {
                $name = $node->nodeName;
                $text = $node->textContent;
                $data[$name] = $text;
            }
        }

        return new Response($data);
    }
1349990
public static function parseExpressionXml(SimpleXMLElement $xml){
        $returnValue = self::parseElementXml($xml);
        $value = trim($xml);
        $expressions = array();
        foreach($xml->children() as $child){
            $expressions[] = self::parseExpressionXml($child);
        }
        if(count($expressions)){
            $returnValue['expressions'] = $expressions;
        }
        if(strlen($value)){
            $returnValue['value'] = $value;
        }
        return $returnValue;
    }
11648
protected function parseLegacyXml($xml)
    {
        $extractedData = array();

        $dom = new \DOMDocument();
        $dom->loadXml($xml);

        $ezimageTag = $dom->documentElement;

        if (!$ezimageTag->hasAttribute('url')) {
            throw new \RuntimeException('Missing attribute "url" in <ezimage/> tag.');
        }

        if (($legacyUrl = $ezimageTag->getAttribute('url')) === '') {
            // Detected XML considered "empty" by the legacy storage
            return null;
        }

        $url = $this->urlRedecorator->redecorateFromTarget($legacyUrl);
        $extractedData['id'] = $this->imageIoService->loadBinaryFileByUri($url)->id;

        if (!$ezimageTag->hasAttribute('filename')) {
            throw new \RuntimeException('Missing attribute "filename" in <ezimage/> tag.');
        }
        $extractedData['fileName'] = $ezimageTag->getAttribute('filename');
        $extractedData['width'] = $ezimageTag->getAttribute('width');
        $extractedData['height'] = $ezimageTag->getAttribute('height');
        $extractedData['mime'] = $ezimageTag->getAttribute('mime_type');

        if (!$ezimageTag->hasAttribute('alternative_text')) {
            throw new \RuntimeException('Missing attribute "alternative_text" in <ezimage/> tag.');
        }
        $extractedData['alternativeText'] = $ezimageTag->getAttribute('alternative_text');

        return $extractedData;
    }
220030
private function processXmlReturn(SimpleXMLElement $xml)
    {
        if (isset($xml->error)) {
            if (isset($xml->error['@attributes'])) {
                $this->lastStatus = (int)$xml->error['@attributes']['code'];
                if (isset($xml->error['@attributes']['resettimer'])) {
                    $this->apiLimitReset
                        = (int)$xml->error['@attributes']['resettimer'];
                }
            }
            throw new RuntimeException($xml->error, $this->lastStatus);
        }

        if (!isset($xml->success)) {
            throw new RuntimeException(
                'Unexpected NMA API response',
                500
            );
        }

        $this->lastStatus = (int)$xml->success['@attributes']['code'];
        $this->apiCallsRemaining
            = (int)$xml->success['@attributes']['remaining'];
        $this->apiLimitReset = (int)$xml->success['@attributes']['resettimer'];

        return $this;
    }
1048943
protected function xml2object($xml)
    {
        $envelope = simplexml_load_string(
            $xml,
            'SimpleXMLElement',
            0,
            'http://schemas.xmlsoap.org/soap/envelope/'
        );

        foreach ($envelope->Body as $response) {
            $children = $response->children(static::XML_NAMESPACE);
            foreach ($children as $item) {
                return $item;
            }
        }

        throw new Exception('XML from gateway could not be parsed');
    }
820
public function xml2array(\SimpleXMLElement $xml)
    {
        $arr = array();
        foreach ($xml as $element) {
            $tag = $element->getName();
            $e = get_object_vars($element);
            if (!empty($e)) {
                $arr[$tag][] = $element instanceof \SimpleXMLElement ? $this->xml2array($element) : $e;
            } else {
                $arr[$tag] = trim($element);
            }
        }

        return $arr;
    }
1334063
private function createDomDocument($xml)
    {
        $domDocument = new \DOMDocument();
        if ($domDocument->loadXML($xml) === false) {
            throw new ParseException("Could not parse xml input");
        }
        return $domDocument;
    }
667631
public function parseXML(\SimpleXMLElement $xml)
    {
        $name = $xml->getName();
        if ($name !== 'svg') {
            return null;
        }

        $width = isset($xml['width']) ? $xml['width'] : null;
        $height = isset($xml['height']) ? $xml['height'] : null;
        $namespaces = $xml->getNamespaces(true);

        $img = new SVG($width, $height, $namespaces);

        $nsKeys = array_keys($namespaces);

        $doc = $img->getDocument();
        $this->applyAttributes($doc, $xml, $nsKeys);
        $this->applyStyles($doc, $xml);
        $this->addChildren($doc, $xml, $nsKeys);

        return $img;
    }
19728
protected function createTemplateFromXml($xml)
    {
        /** @var Template $template */
        $template = $this->serializer->deserialize($xml, 'phpDocumentor\Transformer\Template', 'xml');
        $template->propagateParameters();

        return $template;
    }
764291
private function validate(SimpleXMLElement $xml): void
    {
        if ($xml->getName() !== 'phpdocumentor') {
            throw new InvalidArgumentException(
                sprintf('Root element name should be phpdocumentor, %s found', $xml->getName())
            );
        }
    }
310530
public static function parse_preset($xml) {
        global $DB;

        $info = array();

        if (!self::is_valid_preset($xml)) {
            return null;
        }

        $dom = new DOMDocument();
        $dom->loadXML($xml);

        $info['shortname'] = self::get_node_value($dom, '/role/shortname');
        if (isset($info['shortname'])) {
            $info['shortname'] = strtolower(clean_param($info['shortname'], PARAM_ALPHANUMEXT));
        }

        $info['name'] = self::get_node_value($dom, '/role/name');
        if (isset($value)) {
            $info['name'] = clean_param($info['name'], PARAM_TEXT);
        }

        $info['description'] = self::get_node_value($dom, '/role/description');
        if (isset($value)) {
            $info['description'] = clean_param($info['description'], PARAM_CLEANHTML);
        }

        $info['archetype'] = self::get_node_value($dom, '/role/archetype');
        if (isset($value)) {
            $archetypes = get_role_archetypes();
            if (!isset($archetypes[$info['archetype']])) {
                $info['archetype'] = null;
            }
        }

        $values = self::get_node_children_values($dom, '/role/contextlevels', 'level');
        if (isset($values)) {
            $info['contextlevels'] = array();
            $levelmap = array_flip(context_helper::get_all_levels());
            foreach ($values as $value) {
                $level = 'context_'.$value;
                if (isset($levelmap[$level])) {
                    $cl = $levelmap[$level];
                    $info['contextlevels'][$cl] = $cl;
                }
            }
        }

        foreach (array('assign', 'override', 'switch', 'view') as $type) {
            $values = self::get_node_children_values($dom, '/role/allow'.$type, 'shortname');
            if (!isset($values)) {
                $info['allow'.$type] = null;
                continue;
            }
            $info['allow'.$type] = array();
            foreach ($values as $value) {
                if ($value === $info['shortname']) {
                    array_unshift($info['allow'.$type], -1); // Means self.
                }
                if ($role = $DB->get_record('role', array('shortname'=>$value))) {
                    $info['allow'.$type][] = $role->id;
                    continue;
                }
            }
        }

        $info['permissions'] = array();
        $values = self::get_node_children_values($dom, '/role/permissions', 'inherit');
        if (isset($values)) {
            foreach ($values as $value) {
                if ($value = clean_param($value, PARAM_CAPABILITY)) {
                    $info['permissions'][$value] = CAP_INHERIT;
                }
            }
        }
        $values = self::get_node_children_values($dom, '/role/permissions', 'allow');
        if (isset($values)) {
            foreach ($values as $value) {
                if ($value = clean_param($value, PARAM_CAPABILITY)) {
                    $info['permissions'][$value] = CAP_ALLOW;
                }
            }
        }
        $values = self::get_node_children_values($dom, '/role/permissions', 'prevent');
        if (isset($values)) {
            foreach ($values as $value) {
                if ($value = clean_param($value, PARAM_CAPABILITY)) {
                    $info['permissions'][$value] = CAP_PREVENT;
                }
            }
        }
        $values = self::get_node_children_values($dom, '/role/permissions', 'prohibit');
        if (isset($values)) {
            foreach ($values as $value) {
                if ($value = clean_param($value, PARAM_CAPABILITY)) {
                    $info['permissions'][$value] = CAP_PROHIBIT;
                }
            }
        }

        return $info;
    }
46412
private function convertDocument(\DOMDocument $xml)
    {
        return $this->loader->load($xml->saveXML(), [LoaderInterface::FROM_STRING => true]);
    }