Convert an XML to Array
@param string $input_xml
@return \DOMDocument
@throws \Exception
552806
public static function &createArray($input_xml) {
        $xml = self::getXMLRoot();
        if(is_string($input_xml)) {
            $parsed = $xml->loadXML($input_xml);
            if(!$parsed) {
                throw new \Exception('[XML2Array] Error parsing the XML string.');
            }
        } else {
            if(get_class($input_xml) != 'DOMDocument') {
                throw new \Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
            }
            $xml = self::$xml = $input_xml;
        }
        $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
        self::$xml = null;    // clear the xml node in the class for 2nd time use.
        return $array;
    }
413767
public function loadAsXML()
    {
        $this->checkFileExists();
        $xml = new \DomDocument ();
        libxml_use_internal_errors(true);
        $result = $xml->load($this->absoluteFileName);
        if ($result) {
            return $xml;
        }
        $messageObject = ArrayUtil::getFromArray(libxml_get_errors(), 0);
        $libXMLMessage = ObjectUtil::getFromObject($messageObject, "message");
        $message = sprintf(self::EXCEPTION_INVALID_XML, $this->absoluteFileName, $libXMLMessage);
        $e = new \Exception(libxml_get_errors()[0]->message);
        libxml_clear_errors();
        throw $e;
    }
747476
function inputXML( )
    {
        $node = null;
        $dom = new DOMDocument( '1.0', 'utf-8' );
        $success = false;
        if ( $this->XMLData )
        {
            $success = $dom->loadXML( $this->XMLData );
        }

        eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext',
                                    $this->XMLData,
                                    __METHOD__ . ' xml string stored in database' );

        $output = '';
        $browserEngineName = self::browserSupportsDHTMLType();

        if ( $success )
        {
            $rootSectionNode = $dom->documentElement;
            $output .= $this->inputSectionXML( $rootSectionNode, 0 );
        }

        if ( $browserEngineName === 'Trident' )
        {
            $output = str_replace( '<p></p>', '<p>&nbsp;</p>', $output );
        }
        else /* if ( $browserEngineName !== 'Presto' ) */
        {
            $output = str_replace( '<p></p>', '<p><br /></p>', $output );
        }

        $output = str_replace( array( "\n", "\xC2\xA0" ), array( '', '&nbsp;' ), $output );

        if ( $output )
        {
            if ( $browserEngineName === 'Trident' )
            {
                $output .= '<p>&nbsp;</p>';
            }
            else
            {
                $output .= '<p><br /></p>';
            }
        }

        eZDebugSetting::writeDebug( 'kernel-datatype-ezxmltext',
                                    $output,
                                    __METHOD__ . ' xml output to return' );

        $output = htmlspecialchars( $output, ENT_COMPAT, 'UTF-8' );

        return $output;
    }
1164724
protected static function xmlToArray(\SimpleXMLElement $xml)/*# : array */
    {
        $arr = array();
        foreach ($xml as $element) {
            $tag = $element->getName();
            $ele = get_object_vars($element);
            if (!empty($ele)) {
                $arr[$tag] = $element instanceof \SimpleXMLElement ?
                    static::xmlToArray($element) :
                    $ele;
            } else {
                $arr[$tag] = trim($element);
            }
        }
        return $arr;
    }
1080142
protected function parseXml($input)
    {
        if (class_exists('SimpleXMLElement')) {
            try {
                $backup = libxml_disable_entity_loader(true);
                $result = new \SimpleXMLElement($input);
                libxml_disable_entity_loader($backup);

                return $result;
            } catch (\Exception $e) {
                // Do nothing
            }
        }

        return $input;
    }
381480
public function xml2array($xml)
    {
        $xml = trim($xml);
        if (!$xml) {
            throw new EasyXMLException('You should provide a XML document.');
        }

        try {
            $xml = is_file($xml) ? file_get_contents($xml) : $xml;
            $dom = new DomDocument();
            $dom->loadXML($xml);
            return XML2Array::createArray($dom);
        } catch (\Exception $e) {
            throw new EasyXMLException('Invalid XML document given.');
        }
    }
572925
public static function parseXMLToArray($xml)
    {
        if (empty($xml))
            return false;

        $data = array();
        $data[$xml['Stage']->getName()] = $xml['Stage']->__toString();
        foreach ($xml as $element) {
            if ($element->children()) {
                foreach ($element as $child) {
                    if ($child->attributes()) {
                        foreach ($child->attributes() as $key => $value) {
                            $data[$element->getName()][$child->getName()][$key] = $value->__toString();
                        }
                    } else {
                        $data[$element->getName()][$child->getName()] = $child->__toString();
                    }
                }
            } else {
                $data[$element->getName()] = $element->__toString();
            }
        }

        $data = json_encode($data);
        if(JsonValidator::validate($data))
            return $data;

        return false;
    }
366523
public static function array_to_xml($array, &$xml)
	{
		foreach($array as $key => $value) {
			if(is_array($value)) {
				if(!is_numeric($key)){
					$subnode = $xml->addChild("$key");
					self::array_to_xml($value, $subnode);
				} elseif ($key == 0) {
					self::array_to_xml($value, $xml);
				} else {
					$name = $xml->getName();
					$subnode = $xml->xpath("..")[0]->addChild("$name");
					self::array_to_xml($value, $subnode);
				}
			} else {
				$xml->addChild("$key","$value");
			}
		}
	}
1234432
public function xmlToArray( $string )
    {

        $doc = new DOMDocument();
        $doc->loadXML( $string );
        $root = $doc->documentElement;

        return $this->DomNodeToArray( $root );
    }
89626
private static function xml2array(\SimpleXMLElement $xml)
    {
        $arr = [];

        if ($xml) {
            foreach ($xml->children() as $r) {
                $t = [];
                if (!is_null($r) && count($r->children()) == 0) {
                    $arr[$r->getName()] = strval($r);
                } else {
                    $arr[$r->getName()][] = self::xml2array($r);
                }
            }
        }

        return $arr;
    }
606838
protected static function recursiveXMLToArray($xml)
    {
        $x = null;
        if ($xml instanceof SimpleXMLElement) {
            $attributes = $xml->attributes();
            foreach ($attributes as $k => $v) {
                if ($v) {
                    $a[$k] = (string) $v;
                }
            }
            $x = $xml;
            $xml = get_object_vars($xml);
        }
        if (is_array($xml)) {
            if (count($xml) === 0) {
                return (string)$x;
            } // for CDATA
            $r = [];
            foreach ($xml as $key => $value) {
                $r[$key] = self::recursiveXMLToArray($value);
            }
            // Attributes
            if (isset($a)) {
                $r['@'] = $a;
            }
            return $r;
        }

        return (string) $xml;
    }
430033
public function xmlToArray($xmlObject, $output = array())
    {
        foreach ((array) $xmlObject as $index => $node) {
            $output[$index] = ($node instanceof \SimpleXMLElement || is_array($node))
                ? $this->xmlToArray($node)
                : $node;
        }

        return $output;
    }
26346
public function toXML()
    {
        $namespace = 'tns';
        $writer = new XMLWriter();
        $writer->openMemory();
        $writer->setIndent(true);
        $writer->setIndentString('    ');
        $writer->startElementNs($namespace, 'Naknada', null);
        $writer->writeElementNs($namespace, 'NazivN', null, $this->getName());
        $writer->writeElementNs($namespace, 'IznosN', null, $this->getValue());
        $writer->endElement();

        return $writer->outputMemory();
    }
444854
private function &convert($node_name, $arr = [])
    {
        $xml = $this->getXMLRoot();
        $node = $xml->createElement($node_name);
        if (is_array($arr)) {
            // get the attributes first.;
            if (isset($arr['@attributes'])) {
                foreach ($arr['@attributes'] as $key => $value) {
                    if (!$this->isValidTagName($key)) {
                        throw new Exception('[Array2XML] Illegal character in attribute name.'
                            . 'attribute: ' . $key . ' in node: ' . $node_name);
                    }
                    $node->setAttribute($key, $this->boolString($value));
                }
                unset($arr['@attributes']);
            }
            // check if it has a value stored in @value,
            // if yes store the value and return else check if its directly stored as string
            if (isset($arr['@value'])) {
                $node->appendChild($xml->createTextNode($this->boolString($arr['@value'])));
                unset($arr['@value']);

                return $node;
            } else {
                if (isset($arr['@cdata'])) {
                    $node->appendChild($xml->createCDATASection($this->boolString($arr['@cdata'])));
                    unset($arr['@cdata']);

                    return $node;
                }
            }
        }
        //create subnodes using recursion
        if (is_array($arr)) {
            // recurse to get the node for that key
            foreach ($arr as $key => $value) {
                if (!$this->isValidTagName($key)) {
                    throw new Exception('[Array2XML] Illegal character in tag name.'
                        . 'tag: ' . $key . ' in node: ' . $node_name);
                }
                if (is_array($value) && is_numeric(key($value))) {
                    // MORE THAN ONE NODE OF ITS KIND
                    // if the new array is numeric index, means it is array of nodes of the same kind
                    // it should follow the parent key name
                    foreach ($value as $k => $v) {
                        $node->appendChild($this->convert($key, $v));
                    }
                } else {
                    // ONLY ONE NODE OF ITS KIND
                    $node->appendChild($this->convert($key, $value));
                }
                unset($arr[$key]); //remove the key from the array once done.
            }
        }
        // after we are done with all the keys in the array (if it is one)
        // we check if it has any text value, if yes, append it.
        if (!is_array($arr) && $arr !== '') {
            $node->appendChild($xml->createTextNode($this->boolString($arr)));
        }

        return $node;
    }
572907
public function xml()
    {
        foreach ($this->_result as $ps) {
            $xmlps = $this->xml->addChild("Process");
            $xmlps->addAttribute("Name", $ps[0]);
            $xmlps->addAttribute("Status", $ps[1] ? 1 : 0);
        }

        return $this->xml->getSimpleXmlElement();
    }
339414
public static function createArray($inputXml)
    {
        $xml = self::getXMLRoot();
        if (is_string($inputXml)) {
            $parsed = $xml->loadXML($inputXml);
            if (!$parsed) {
                throw new \InvalidArgumentException('[XML2Array] Error parsing the XML string.');
            }
        } else {
            if (get_class($inputXml) !== 'DOMDocument') {
                throw new \InvalidArgumentException('[XML2Array] The input XML object should be of type: DOMDocument.');
            }
            $xml = self::$xml = $inputXml;
        }
        $array = [];
        $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
        self::$xml = null;// clear the xml node in the class for 2nd time use.

        return $array;
    }
1273253
public function fromXml(string $xml): array
    {
        if (!$this->isEmptyValue($xml)) {

            libxml_use_internal_errors(true);

            // convert string to xml object
            $result = simplexml_load_string("$xml", 'SimpleXMLElement', LIBXML_NOCDATA);

            if ($result) {
                return $this->fromClass($result);

            } else {

                foreach (libxml_get_errors() as $error) {

                    // if don`t have container add that`s
                    if ($error->code == 5) {
                        $this->errors = [];

                        // append container and parse again
                        return $this->fromXml("<DATA>$xml</DATA>");

                    } else
                        $this->setError($error->code, $error->message);
                }
            }
        }

        return $this->result;
    }