» » Conversion between PHP array and XML

 

Conversion between PHP array and XML

Author: bamboo06 on 9-04-2017, 00:59, views: 1614

1
In the development, we often encounter the conversion between the array and XML, especially when dealing with interface development often used, such as the other side of the client POST a XML format data to the server, the server program is responsible for receiving Analysis, there is a need to data table data in XML format to provide third-party applications.
Conversion between PHP array and XML

In this article we will briefly explain how to use PHP to deal with the conversion between the array and XML.

PHP converts an array to XML
PHP can convert the array into xml format, the simple way is to traverse the array, and then the array of key / value into xml node, and then directly echo output, such as:
function arrayToXml($arr){ 
    $xml = "<root>"; 
    foreach ($arr as $key=>$val){ 
        if(is_array($val)){ 
            $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; 
        }else{ 
            $xml.="<".$key.">".$val."</".$key.">"; 
        } 
    } 
    $xml.="</root>"; 
    return $xml; 
} 

I tested the next, the most simple, fast and fast, support for the array.
Another way is to use DOMDocument to generate xml structure:
function arrayToXml($arr,$dom=0,$item=0){ 
    if (!$dom){ 
        $dom = new DOMDocument("1.0"); 
    } 
    if(!$item){ 
        $item = $dom->createElement("root");  
        $dom->appendChild($item); 
    } 
    foreach ($arr as $key=>$val){ 
        $itemx = $dom->createElement(is_string($key)?$key:"item"); 
        $item->appendChild($itemx); 
        if (!is_array($val)){ 
            $text = $dom->createTextNode($val); 
            $itemx->appendChild($text); 
             
        }else { 
            arrayToXml($val,$dom,$itemx); 
        } 
    } 
    return $dom->saveXML(); 
} 

It can also convert an array to xml and support multidimensional arrays.

PHP converts XML into an array
Do interface development often encounter someone else to submit to you is the xml format data, the common WeChat interface, Alipay interface, etc., their interface, such as sending messages are xml format, then we first find a way to get this Xml data, and then convert it into an array.
Suppose we get an XML like this:
<root> 
<user>goocode</user> 
<pvs>12345</pvs> 
<ips> 
<google_ip>1200</google_ip> 
<bing_ip>1829</bing_ip> 
</ips> 
<date>2017-04-01</date> 
</root> 

Through simplexml_load_string () parsing read xml data, and then first converted to json format, and then converted into an array.
function xmlToArray($xml){     
    //Do not reference external xml entities
    libxml_disable_entity_loader(true); 
    $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
    $val = json_decode(json_encode($xmlstring),true);   
    return $val; 
} 

Call the function to get the array, we can carry out a variety of data processing.

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
Today, 09:41

bennieben

Reply
  • Group: Guests
  • –†Registered date: --
  • Status:
 
this is a great post. i had this issue when i was working at BritishEssays.net, and now I get what was wrong. thanks a lot! stay strong my dude!


Add comments

Name:*
E-Mail:
Comment:
Bold Italic Underline Strike | Align left Center Align right | Insert smilies Insert link URLInsert protected URL Select color | Add Hidden Text Insert Quote Convert selected text from selection to Cyrillic (Russian) alphabet Insert spoiler
Security Code: *