Advertisement
halleman19

convert php array to xml | unity3d

Dec 23rd, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | Gaming | 0 0
  1. <?php
  2.  
  3.     include_once('db.class.php');
  4.    
  5.     $connInfo = array('user' => '', 'pass' => '', 'host' => '', 'name' => '');
  6.    
  7.     $db = new Db($connInfo);
  8.    
  9.     function printResponse($status, $data)
  10.     {
  11.         $xmlDoc = new DOMDocument();
  12.         $xmlDoc->encoding = 'UTF-8';
  13.        
  14.         $root = $xmlDoc->appendChild($xmlDoc->createElement('root'));
  15.        
  16.         $root->appendChild($xmlDoc->createElement('status', $status));
  17.        
  18.         $infoBl = $root->appendChild($xmlDoc->createElement('response'));
  19.  
  20.         foreach($data as $key => $val)
  21.             $infoBl->appendChild($xmlDoc->createElement($key, $val));
  22.        
  23.         header("Content-Type: text/plain");
  24.         $xmlDoc->formatOutput = true;
  25.        
  26.         die($xmlDoc->saveXml());
  27.     }
  28.    
  29.     function printListResponse($status, $data, $itemTitle)
  30.     {
  31.         $xmlDoc = new DOMDocument();
  32.        
  33.         $root = $xmlDoc->appendChild($xmlDoc->createElement('root'));
  34.         $root->appendChild($xmlDoc->createElement('status', $status));
  35.        
  36.         $items = $root->appendChild($xmlDoc->createElement('response'));
  37.        
  38.         foreach($data as $item)
  39.         {
  40.             $el = $items->appendChild($xmlDoc->createElement($itemTitle));
  41.            
  42.             foreach($item as $key => $val)
  43.                 $el->appendChild($xmlDoc->createElement($key, $val));
  44.         }
  45.        
  46.         header("Content-Type: text/plain");
  47.         $xmlDoc->formatOutput = true;
  48.        
  49.         die($xmlDoc->saveXml());
  50.     }
  51.  
  52.     // example: https://imgur.com/a/qtsLMhE
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement