程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP XML轉換為數組函數

PHP XML轉換為數組函數

編輯:關於PHP編程

PHP XML轉換為數組函數

  1. <?php
  2.  function xml2array($originalXML, $attributes=true)
  3.  {
  4.          $xmlArray = array();
  5.          $search = $attributes ? |<((S )(.*))s*>(.*)</<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>>|Ums : |<((S )()).*>(.*)</<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>>|Ums;
  6.        
  7.          // normalize data
  8.          $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
  9.          $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
  10.          $xml = preg_replace(|<(S ?)(.*)/>|U, <[code]<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>></[code]>, $xml); //Expand singletons
  11.        
  12.          if (! preg_match_all($search, $xml, $xmlMatches))
  13.                  return trim($originalXML);      // bail out - no XML found
  14.                
  15.          foreach ($xmlMatches[1] as $index => $key)
  16.          {
  17.                  if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
  18.                  $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
  19.    &nb

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved