php分析xml執行個體程式

來源:互聯網
上載者:User

<?xml version='1.0' encoding='UTF-8'?>
<root>
        <key id="5201314" name="望咩掉啊">
                <user id='6789' name='不上學了'><![CDATA[我靠!!!@*&^$(*%&]]></key>
                <user id="6789" name="不上學了">222</key>
        </key>
        <key id="5201314" name="望咩掉啊">
                <user id='6789' name='不上學了'><![CDATA[我靠!!!|""!@":#$@$]]></key>
                <user id="6789" name="不上學了">222</key>
        </key>
        <key id="5201314" name="望咩掉啊">
                <user id='6789' name='不上學了'><![CDATA[我靠!!!<><><><>]]></key>
                <user id="6789" name="不上學了">222</key>
        </key>
</root>

<?php
/*  func.xml.php  2008-09-20  kilobug
必須聲明頭部屬性,xml每個節點的值和屬性嚴禁出現 <、>、" 字元,如需出現,請使用CDATA
每個節點屬性必須統一使用單引號'或雙引號"
忽略注釋,返回一個數組。
*/

$xmlContent = ''; //臨時變數

/* 載入xml檔案,解析後返回數組 */
function loadXML( $path, $assoc = null, $attrassoc = null ) {
return parseXML( @file_get_contents( $path ), $assoc, $attrassoc );
}
/* 解析XMl內容,返回數組 */
function parseXML( $xmltext, $assoc = null, $attrassoc = null ) {
$xmldata = array();
//轉換頂部屬性
$noderoot = strpos( $xmltext, '<?' );
$noderootspace = strpos( $xmltext, ' ' );
$nodefoot = strpos( $xmltext, '?>' );
$attr = trim( substr($xmltext, $noderootspace, $nodefoot-$noderoot-5) );
//轉換節點
$xmldata = array_merge(
   array(
    array_merge(
     array( substr($xmltext, $noderoot+2, $noderootspace-$noderoot-2) ),
     parseXMLAttrs($attr, $attrassoc)
    )
   ),
   parseXMLNodes( trim(substr($xmltext, $nodefoot+2)), $assoc, $attrassoc )
     );
return $xmldata;
}
/* 解析XML節點,返回數組 */
function parseXMLNodes( $nodestr, $assoc = null, $attrassoc = null ) {
global $xmlContent;
$xmlContent = $nodestr;
if ( empty($xmlContent) || !strstr($xmlContent, '<') ) {
  return array();
}
$nodedate = array();
$i = 0;
while ( !empty($xmlContent) ) {
  $noderoot = strpos( $xmlContent, '<' );
  $nodefoot = strpos( $xmlContent, '>' );
  $nodeend = strpos( $xmlContent, '/' );
  $cdataroot = strpos( $xmlContent, '<![CDATA[' );
  $cdatafoot = strpos( $xmlContent, ']]>' );
  if ( $noderoot === $cdataroot ) { //判斷是否CDATA
   $nodedate = substr($xmlContent, $cdataroot+9, $cdatafoot-$cdataroot-9);
   $xmlContent = substr( $xmlContent, strpos($xmlContent, '>', $cdatafoot+3)+1 );
   break $nodedate;
  } else if ( $noderoot == $nodeend-1 ) { //是否結束節點,是則返回前面的內容
   if ( $i == 0 ) {
    $nodedate = substr( $xmlContent, 0, $noderoot );
    $xmlContent = substr( $xmlContent, $nodefoot+1 );
    return $nodedate;
   }
   else {
    $xmlContent = substr( $xmlContent, $nodefoot+1 );
    break;
   }
  }
  $nodecompart = strpos( $xmlContent, ' ' );
  //判斷有無屬性
  if ( $nodecompart != false && $nodecompart > $noderoot && $nodecompart < $nodefoot ) { //有屬性
   $nodedate[$i][0] = array_merge( array($assoc ? substr($xmlContent, $noderoot+1, $nodecompart-$noderoot-1) : ''), parseXMLAttrs(substr($xmlContent, $nodecompart+1, $nodefoot-$nodecompart-($nodefoot-1 == $nodeend ? 2 : 1)), $attrassoc) );
  
  } else { //無屬性
   $nodedate[$i][0] = array( $assoc ? substr($xmlContent, $noderoot+1, $nodefoot-$noderoot-1) : '' );
  }
  $i++;
  $xmlContent = substr( $xmlContent, $nodefoot+1 ); //截取本節點後面內容
  if ( $nodefoot-1 == $nodeend ) { //判斷是否單節點
   $nodedate[$i-1][1] = '';
   continue;
  } else {
   $nodedate[$i-1][1] = parseXMLNodes( $xmlContent, $assoc, $attrassoc );
  }
}
return $nodedate;
}
/* 解析XML節點屬性,返回數組 */
function parseXMLAttrs( $attrstr, $assoc = null ) {
if ( !$attrstr ) return array();
echo( $attrstr . "n" );
$attrdata = array();
$attrcenter = $attrfoot = 1; //開始轉換
$tmpnum = false;
while ( $attrcenter != false ) {
  $tmpnum = strpos($attrstr, '='');
  $attrcenter = strpos($attrstr, '="');
  if ( $tmpnum == false ) {
   $attrfoot = strpos($attrstr, '"', $attrcenter+2);
  } else {
   $attrcenter = $tmpnum;
   $attrfoot = strpos($attrstr, ''', $attrcenter+2);
  }
  if ( $assoc ) { //是否關聯陣列
   $attrdata[substr( $attrstr, 0, $attrcenter)] = substr( $attrstr, $attrcenter+2, $attrfoot-$attrcenter-2 );
  } else {
   $attrdata[] = substr( $attrstr, $attrcenter+2, $attrfoot-$attrcenter-2 );
  }
  $attrstr = trim(substr($attrstr, $attrfoot+1));
  if ( !$attrstr ) {
   break;
  }
}
return $attrdata;
}

//解析數組為XML(沒寫)
function parseArrayToXML( $ary ) {
       
}
//解析數組為XML的Nodes(沒寫)
function parseArrayToXMLNodes( $ary ) {
       
}

/* 測試
$t = microtime();
//loadXML('../books.xml');
print_r( loadXML('../test.xml', 1, 1) ); //自己找一個常見的xml文檔
echo microtime() - $t;
*/

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.