How to convert array to SimpleXML

前端 未结 30 2614
遇见更好的自我
遇见更好的自我 2020-11-21 06:52

How can I convert an array to a SimpleXML object in PHP?

相关标签:
30条回答
  • 2020-11-21 07:30

    From PHP 5.4

    function array2xml($data, $root = null){
        $xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '<root/>');
        array_walk_recursive($data, function($value, $key)use($xml){
            $xml->addChild($key, $value);
        });
        return $xml->asXML();
    }
    
    0 讨论(0)
  • 2020-11-21 07:32

    You could use the XMLParser that I have been working on.

    $xml = XMLParser::encode(array(
        'bla' => 'blub',
        'foo' => 'bar',
        'another_array' => array (
            'stack' => 'overflow',
        )
    ));
    // @$xml instanceof SimpleXMLElement
    echo $xml->asXML();
    

    Would result in:

    <?xml version="1.0"?>
    <root>
        <bla>blub</bla>
        <foo>bar</foo>
        <another_array>
            <stack>overflow</stack>
        </another_array>
    </root>
    
    0 讨论(0)
  • 2020-11-21 07:33
    function array2xml($array, $xml = false){
    
        if($xml === false){
    
            $xml = new SimpleXMLElement('<?xml version=\'1.0\' encoding=\'utf-8\'?><'.key($array).'/>');
            $array = $array[key($array)];
    
        }
        foreach($array as $key => $value){
            if(is_array($value)){
                $this->array2xml($value, $xml->addChild($key));
            }else{
                $xml->addChild($key, $value);
            }
        }
        return $xml->asXML();
    }
    
    0 讨论(0)
  • 2020-11-21 07:33

    If you work in magento and you have this type of associative array

    $test_array = array (
        '0' => array (
                'category_id' => '582',
                'name' => 'Surat',
                'parent_id' => '565',
                'child_id' => '567',
                'active' => '1',
                'level' => '6',
                'position' => '17'
        ),
    
        '1' => array (
                'category_id' => '567', 
                'name' => 'test',
                'parent_id' => '0',
                'child_id' => '576',
                'active' => '0',
                'level' => '0',
                'position' => '18'
        ),
    );
    

    then this is best to convert associative array to xml format.Use this code in controller file.

    $this->loadLayout(false);
    //header ("content-type: text/xml");
    $this->getResponse()->setHeader('Content-Type','text/xml');
    $this->renderLayout();
    
    $clArr2xml = new arr2xml($test_array, 'utf-8', 'listdata');
    $output = $clArr2xml->get_xml();
    print $output; 
    
    class arr2xml
    {
    var $array = array();
    var $xml = '';
    var $root_name = '';
    var $charset = '';
    
    public function __construct($array, $charset = 'utf-8', $root_name = 'root')
    {
        header ("content-type: text/xml");
        $this->array = $array;
        $this->root_name = $root_name;
        $this->charset = $charset;
    
        if (is_array($array) && count($array) > 0) {
            $this->struct_xml($array);
    
        } else {
            $this->xml .= "no data";
        }
    }
    
    public function struct_xml($array)
    {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                $tag = ereg_replace('^[0-9]{1,}', 'item', $k); // replace numeric key in array to 'data'
                $this->xml .= "<$tag>";
                $this->struct_xml($v);
                $this->xml .= "</$tag>";
            } else {
                $tag = ereg_replace('^[0-9]{1,}', 'item', $k); // replace numeric key in array to 'data'
                $this->xml .= "<$tag><![CDATA[$v]]></$tag>";
            }
        }
    }
    
    public function get_xml()
    {
    
        $header = "<?xml version=\"1.0\" encoding=\"" . $this->charset . "\"?><" . $this->root_name . ">";
        $footer = "</" . $this->root_name . ">";
    
        return $header . $this->xml . $footer;
    }
    }
    

    I hope it helps to all.

    0 讨论(0)
  • 2020-11-21 07:34
    function toXML($data, $obj = false, $dom) {
        $is_first_level = false;
        if($obj === false) {
            $dom = new DomDocument('1.0');
            $obj = $dom;
            $is_first_level = true;
        }
    
        if(is_array($data)) {
            foreach($data as $key => $item) {
                $this->toXML($item, $obj->appendChild($dom->createElement($key)), $dom);
            }
        }else {
            $obj->appendChild($dom->createTextNode($data));
        }
    
        if($is_first_level) {
            $obj->formatOutput = true;
            return $obj->saveXML();
        }
        return $obj;
    }
    
    0 讨论(0)
  • 2020-11-21 07:34

    My answer, cobbling together others' answers. This should correct for the failure to compensate for numeric keys:

    function array_to_xml($array, $root, $element) {
        $xml = new SimpleXMLElement("<{$root}/>");
        foreach ($array as $value) {
            $elem = $xml->addChild($element);
            xml_recurse_child($elem, $value);
        }
        return $xml;
    }
    
    function xml_recurse_child(&$node, $child) {
        foreach ($child as $key=>$value) {
            if(is_array($value)) {
                foreach ($value as $k => $v) {
                    if(is_numeric($k)){
                        xml_recurse_child($node, array($key => $v));
                    }
                    else {
                        $subnode = $node->addChild($key);
                        xml_recurse_child($subnode, $value);
                    }
                }
            }
            else {
                $node->addChild($key, $value);
            }
        }   
    }
    

    The array_to_xml() function presumes that the array is made up of numeric keys first. If your array had an initial element, you would drop the foreach() and $elem statements from the array_to_xml() function and just pass $xml instead.

    0 讨论(0)
提交回复
热议问题