Convert XML to Array
555745
public function xml2array(): void
    {
        $this->xml2json();
        $this->arrayData = json_decode($this->jsonData, TRUE);
    }
636314
protected static function arrayToXML($data, &$xml = null){

            if($xml === null)
                $xml = new \SimpleXMLElement("<?xml version=\"1.0\"?><nondb></nondb>");
            foreach($data as $k=>$v){
                if(is_array($v)){
                    $sub = $xml->addChild("$k");
                    self::arrayToXML($v, $sub);
                }else{
                    $xml->addChild("$k", htmlspecialchars("$v"));
                }
            }

            return $xml->asXML();

        }
655415
protected function xmlToArray($xml)
    {
        // Do not output libxml error messages to screen
        $useErrors = libxml_use_internal_errors(true);
        $array = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        libxml_use_internal_errors($useErrors);

        // Fix the issue that XML parse empty data to new SimpleXMLElement object
        return array_map('strval', (array) $array);
    }
518558
public function toArray( $xml )
	{
		$this->data	= array();
		$this->xml	= xml_parser_create();
		xml_set_object( $this->xml, $this );
		xml_set_element_handler( $this->xml, 'handleTagOpenForArray', 'handleTagCloseForArray' );
		xml_set_character_data_handler( $this->xml, 'handleCDataForArray' );
		$this->last	= array( &$this->data );
		if( !xml_parse( $this->xml, $xml ) )
		{
			$msg	= "XML error: %s at line %d";
			$error	= xml_error_string( xml_get_error_code( $this->xml ) );
			$line	= xml_get_current_line_number( $this->xml );
			throw new RuntimeException( sprintf( $msg, $error, $line ) );
		}
		xml_parser_free( $this->xml );
		return $this->data;
	}
442314
private static function _unserializeRecursive(SimpleXMLElement $xml)
    {
        $result = [];
        $dataNamespace = Resources::DS_XML_NAMESPACE;
        /** @var SimpleXMLElement $child */
        foreach ($xml->children($dataNamespace) as $child) {
            if (count($child->children($dataNamespace)) > 0) {
                $value = [];
                $children = $child->children($dataNamespace);
                /** @var SimpleXMLElement $firstChild */
                $firstChild = $children[0];
                if ($firstChild->getName() == 'element') {
                    foreach ($children as $subChild) {
                        $value[] = self::_unserializeRecursive(
                            $subChild
                        );
                    }
                } else {
                    $value = self::_unserializeRecursive(
                        $child
                    );
                }
            } else {
                $value = (string) $child;
            }

            $result[$child->getName()] = $value;
        }

        return $result;
    }
955667
protected function convertXmlObjectToArray($xml)
    {
        $array = (array) $xml;

        if (count($array) == 0 && ! is_array($xml)) {

            $array = [ (string) $xml ];
        }

        if (is_array($array)) {

            foreach ($array as $key => $value) {

                if (is_object($value)) {

                    if (strpos(get_class($value), "SimpleXML") !== false) {

                        $array[ $key ] = $this->convertXmlObjectToArray($value);

                    } else {

                        $array[ $key ] = $this->convertXmlObjectToArray( (array) $value );
                    }

                } elseif (is_array($value)) {

                    $array[ $key ] = $this->convertXmlObjectToArray($value);
                }
            }
        }

        return $array;
    }
997999
protected function childToArray(SimpleXmlElement $xml, $name)
    {
        if (!isset($xml->{$name})) {
            return array();
        }

        return $xml->{$name}->toPhpValue('array');
    }
1186665
function toArray()
    {
        $out = array ();

        $attributes = $this->attribute( 'attributes' );
        if ( count( $attributes ) )
        {
            $out['attributes'] = $attributes;
        }

        $children = $this->attribute( 'children' );
        foreach( $children as $name => $value )
        {
            if ( is_object( $value ) )
            {
                $out['children'][$name] = $value->toArray();
            }
            else if ( is_array( $value ) )
            {
                foreach( $value as $key => $val )
                {
                    if ( is_object( $val ) )
                    {
                        $value[$key] = $val->toArray();
                    }
                }
                $out['children'][$name] = $value;
            }
            else
            {
                $out['children'][$name] = $value;
            }
        }

        if ( !count( $children ) )
        {
            $textContent = $this->attribute( 'textContent' );
            if ( $textContent != '' )
            {
                $out['textContent'] = $textContent;
            }
        }

        return $out;
    }
610194
public static function fromXml($xml): array
    {
        if (!$xml) {
            throw new InvalidArgumentException('Convert To Array Error! Invalid Xml!');
        }

        libxml_disable_entity_loader(true);

        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
    }
949521
protected function arrayToXml($result, &$xml)
	{
		foreach ($result as $key => $value) {
			if (is_array($value)) {
				if (!is_numeric($key)) {
					$subnode = $xml->addChild((string) $key);
				} else {
					$subnode = $xml->addChild('item_' . $key);
				}
				$this->arrayToXml($value, $subnode);
			} else {
				$xml->addChild($key, $value);
			}
		}
	}
54348
public static function toXml($params)
    {
        $xml = '<xml>';
        foreach ($params as $key => $val) {
            if (is_numeric($val)) {
                $xml .= '<'.$key.'>'.$val.'</'.$key.'>';
            } else {
                $xml .= '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
            }
        }
        $xml .= '</xml>';
        return $xml;
    }
57552
public function to_array($xml){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }
354579
protected function simpleXmlToArray(SimpleXMLElement $xml) {
		$result = [];
		
		foreach ($xml as $element) {
			$name    = $element->getName();
			$content = get_object_vars($element);
			
			if (isset($content['@attributes'])){
				if (isset($content['@attributes']['value'])) {
					$result[$name] = Std::parseValue($content['@attributes']['value']);
					continue;
				}
				
				unset($content['@attributes']);
			}
			
			if (empty($content)) {
				$result[$name] = Std::parseValue($element);
				continue;
			}
			
			$result[$name] = $this->simpleXmlToArray($element);
		}
		
		return $result;
	}
1150242
public static function xml2array($xml)
    {
        $arr = [];
        if (!empty($xml)) {
            if (is_string($xml)) {
                $xml = simplexml_load_string($xml);
            }
            foreach ($xml->attributes() as $a) {
                $arr['@'.$a->getName()] = strval($a);
            }
            foreach ($xml->children() as $r) {
                if (count($r->children()) == 0) {
                    $arr[$r->getName()] = strval($r);
                } else {
                    $arr[$r->getName()][] = self::xml2array($r);
                }
            }
        }
        return $arr;
    }
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");
			}
		}
	}
1300293
public function toArray()
    {
        $response = $this->simpleXmlObject();
        
        // Converting the SimpleXMLElement Object to array()
        $response = json_encode($response);
        
        return (json_decode($response, true));
    }
1056549
public static function arrayToXml($arr, $num_prefix = "num_") : string
    {
        if (!is_array($arr)) return $arr;
        $result = '';
        foreach ($arr as $key => $val) {
            $key = (is_numeric($key) ? $num_prefix . $key : $key);

            $result .= '<' . $key . '>' . self::arrayToXml($val, $num_prefix) . '</' . $key . '>';
        }
        return $result;
    }