完美XML解析为数组

class ParserXml
{
   function ParserXml()
   {
   
   }
   functionParserXmlByUrl($url)
   {
     $fp = @fopen($url,'r');
   if (!$fp){
    return 1;
   }
   $xmlstr =fread($fp,1024);
   @fclose($fp);
   if ($xmlstr === false){
    return 0;
   }
   return$this->GetXMLTree($xmlstr);
   }
   functionGetXMLTree($xmldata)
   {
    ini_set ('track_errors', '1');
    $xmlreaderror = false;
    $parser = xml_parser_create();
    xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
    if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)){
       $xmlreaderror = true;
       return 0;
    }
    xml_parser_free($parser);
    if(!$xmlreaderror){
       $result = array ();
       $i = 0;
       if (isset($vals[$i]['attributes'])){
           foreach (array_keys ($vals [$i]['attributes']) as $attkey)
           $attributes[$attkey] = $vals[$i]['attributes'][$attkey];
    }   
       $result[$vals [$i]['tag']] = array_merge((array)$attributes,$this->GetChildren($vals, $i, 'open'));
    }
    ini_set('track_errors', '0');
    return $result;
   }
   function GetChildren ($vals,&$i, $type)
   {
    if ($type == 'complete') {
       if (isset ($vals [$i]['value']))
           return ($vals [$i]['value']);
       else
           return '';
   }
   $children = array ();
   while ($vals [++$i]['type'] !='close') {
       $type = $vals [$i]['type'];
       if (isset ($children [$vals [$i]['tag']])) {
           if (is_array ($children [$vals [$i]['tag']])) {
               $temp = array_keys ($children [$vals [$i]['tag']]);
               if (is_string ($temp [0])) {
                   $a = $children [$vals [$i]['tag']];
                   unset ($children [$vals [$i]['tag']]);
                   $children [$vals [$i]['tag']][0] = $a;
               }
           } else {
               $a = $children [$vals [$i]['tag']];
               unset ($children [$vals [$i]['tag']]);
               $children [$vals [$i]['tag']][0] = $a;
           }
           $children [$vals [$i]['tag']][] = $this->GetChildren($vals, $i, $type);
       } else
           $children [$vals [$i]['tag']] = $this->GetChildren($vals, $i, $type);
       if (isset ($vals [$i]['attributes'])) {
           $attributes = array ();
           foreach (array_keys ($vals [$i]['attributes']) as $attkey)
           $attributes [$attkey] = $vals [$i]['attributes'][$attkey];
           if (isset ($children [$vals [$i]['tag']])) {
               if ($children [$vals [$i]['tag']] == '') {
                   unset ($children [$vals [$i]['tag']]);
                   $children [$vals [$i]['tag']] = $attributes;
               }
               elseif (is_array ($children [$vals [$i]['tag']])) {
                   $index = count ($children [$vals [$i]['tag']]) - 1;
                   if ($children [$vals [$i]['tag']][$index] == '') {
                       unset ($children [$vals [$i]['tag']][$index]);
                       $children [$vals [$i]['tag']][$index] = $attributes;
                   }
                   $children [$vals [$i]['tag']][$index] = array_merge ($children[$vals [$i]['tag']][$index], $attributes);
               } else {
                   $value = $children [$vals [$i]['tag']];
                   unset ($children [$vals [$i]['tag']]);
                   $children [$vals [$i]['tag']]['value'] = $value;
                   $children [$vals [$i]['tag']] = array_merge ($children [$vals[$i]['tag']], $attributes);
               }
           } else
               $children [$vals [$i]['tag']] = $attributes;
       }
   }
    return $children;
   }
}

  

转载于:https://www.cnblogs.com/panxiongfei/p/3482766.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值